import { Entity, PrimaryGeneratedColumn, Column, CreateDateColumn, Index } from 'typeorm';

export enum CreditBalanceChangeType {
  SET_LIMIT = 'SET_LIMIT',
  PAYMENT = 'PAYMENT',
  CHECKOUT = 'CHECKOUT',
  CANCEL_ORDER = 'CANCEL_ORDER',
  REFUND = 'REFUND',
  ADJUST = 'ADJUST'
}

@Entity('credit_balance_log_mt')
@Index('idx_tenant_user', ['tenantId', 'userId'])
export class CreditBalanceLogMt {
  @PrimaryGeneratedColumn({ unsigned: true })
  id!: number;

  @Column({
    name: 'tenant_id',
    type: 'int',
    unsigned: true,
    nullable: false,
    comment: '租户ID'
  })
  tenantId!: number;

  @Column({
    name: 'user_id',
    type: 'int',
    unsigned: true,
    nullable: false,
    comment: '用户ID'
  })
  userId!: number;

  @Column({
    name: 'change_type',
    type: 'varchar',
    length: 20,
    nullable: false,
    comment: '变更类型: SET_LIMIT(设置额度), PAYMENT(支付扣减), CHECKOUT(结账恢复), CANCEL_ORDER(取消订单恢复), REFUND(退款恢复), ADJUST(调整额度)'
  })
  changeType!: CreditBalanceChangeType;

  @Column({
    name: 'change_amount',
    type: 'decimal',
    precision: 10,
    scale: 2,
    nullable: false,
    comment: '变更金额（正数表示增加额度，负数表示减少额度）'
  })
  changeAmount!: number;

  @Column({
    name: 'before_total',
    type: 'decimal',
    precision: 10,
    scale: 2,
    nullable: true,
    comment: '变更前总额度'
  })
  beforeTotal!: number | null;

  @Column({
    name: 'after_total',
    type: 'decimal',
    precision: 10,
    scale: 2,
    nullable: true,
    comment: '变更后总额度'
  })
  afterTotal!: number | null;

  @Column({
    name: 'before_used',
    type: 'decimal',
    precision: 10,
    scale: 2,
    nullable: true,
    comment: '变更前已用额度'
  })
  beforeUsed!: number | null;

  @Column({
    name: 'after_used',
    type: 'decimal',
    precision: 10,
    scale: 2,
    nullable: true,
    comment: '变更后已用额度'
  })
  afterUsed!: number | null;

  @Column({
    name: 'reference_id',
    type: 'varchar',
    length: 100,
    nullable: true,
    comment: '关联ID(订单号等)'
  })
  @Index('idx_reference')
  referenceId!: string | null;

  @Column({
    name: 'remark',
    type: 'varchar',
    length: 500,
    nullable: true,
    comment: '备注'
  })
  remark!: string | null;

  @Column({
    name: 'operator_id',
    type: 'int',
    unsigned: true,
    nullable: true,
    comment: '操作人ID'
  })
  operatorId!: number | null;

  @CreateDateColumn({
    name: 'created_at',
    type: 'timestamp',
    comment: '创建时间'
  })
  @Index('idx_created')
  createdAt!: Date;
}