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

@Entity('credit_balance_mt')
@Unique('uk_tenant_user', ['tenantId', 'userId'])
export class CreditBalanceMt {
  @PrimaryGeneratedColumn({ unsigned: true })
  id!: number;

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

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

  @Column({
    name: 'total_limit',
    type: 'decimal',
    precision: 10,
    scale: 2,
    default: 0.00,
    comment: '总额度'
  })
  totalLimit!: number;

  @Column({
    name: 'used_amount',
    type: 'decimal',
    precision: 10,
    scale: 2,
    default: 0.00,
    comment: '已用额度'
  })
  usedAmount!: number;

  @Column({
    name: 'available_amount',
    type: 'decimal',
    precision: 10,
    scale: 2,
    generatedType: 'STORED',
    asExpression: 'total_limit - used_amount',
    comment: '可用额度'
  })
  availableAmount!: number;

  @Column({
    name: 'is_enabled',
    type: 'smallint',
    default: 1,
    comment: '是否启用(0:禁用,1:启用)'
  })
  isEnabled!: number;

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

  @UpdateDateColumn({
    name: 'updated_at',
    type: 'timestamp',
    comment: '更新时间'
  })
  updatedAt!: Date;
}