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

@Entity('merchant_accounts')
@Index(['merchant_id'], { unique: true })
export class MerchantAccount {
  @PrimaryGeneratedColumn({ type: 'bigint', unsigned: true })
  id: number;

  @Column({ type: 'bigint', unsigned: true, comment: '商家ID' })
  merchant_id: number;

  @Column({ type: 'decimal', precision: 12, scale: 2, default: 0, comment: '可用余额' })
  balance: number;

  @Column({ type: 'decimal', precision: 12, scale: 2, default: 0, comment: '冻结余额（提现中）' })
  frozen_balance: number;

  @Column({ type: 'decimal', precision: 12, scale: 2, default: 0, comment: '欠款金额（退款扣回）' })
  debt_amount: number;

  @Column({ type: 'decimal', precision: 12, scale: 2, default: 0, comment: '累计收入（订单结算）' })
  total_income: number;

  @Column({ type: 'decimal', precision: 12, scale: 2, default: 0, comment: '累计支出（提现+退款扣回）' })
  total_expense: number;

  @Column({ type: 'decimal', precision: 12, scale: 2, default: 0, comment: '累计结算金额' })
  total_settlement: number;

  @Column({ type: 'decimal', precision: 12, scale: 2, default: 0, comment: '累计提现金额' })
  total_withdraw: number;

  @Column({ type: 'datetime', nullable: true, comment: '最后结算时间' })
  last_settlement_at: Date;

  @Column({ type: 'decimal', precision: 12, scale: 2, default: 0, comment: '待结算金额' })
  pending_settlement: number;

  @VersionColumn({ comment: '乐观锁版本号' })
  version: number;

  @Column({
    type: 'enum',
    enum: ['active', 'frozen', 'closed'],
    default: 'active',
    comment: '状态'
  })
  @Index()
  status: 'active' | 'frozen' | 'closed';

  @CreateDateColumn({ comment: '创建时间' })
  created_at: Date;

  @UpdateDateColumn({ comment: '更新时间' })
  updated_at: Date;
}
