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

@Entity('system_transactions')
@Index('uk_transaction_no', ['transactionNo'], { unique: true })
@Index('idx_account_id', ['accountId'])
@Index('idx_transaction_type', ['transactionType'])
@Index('idx_business', ['businessType', 'businessId'])
@Index('idx_created_at', ['createdAt'])
export class SystemTransaction {
  @PrimaryGeneratedColumn({ type: 'bigint', unsigned: true, comment: '流水ID' })
  id: number;

  @Column({ type: 'varchar', length: 32, name: 'transaction_no', comment: '交易流水号（全局唯一）' })
  transactionNo: string;

  @Column({ type: 'bigint', unsigned: true, name: 'account_id', comment: '系统账户ID' })
  accountId: number;

  @Column({ type: 'enum', enum: ['income', 'expense'], comment: '方向：income-收入/expense-支出' })
  direction: 'income' | 'expense';

  @Column({ type: 'decimal', precision: 12, scale: 2, comment: '金额（正数）' })
  amount: number;

  @Column({ type: 'decimal', precision: 12, scale: 2, name: 'balance_before', comment: '交易前余额' })
  balanceBefore: number;

  @Column({ type: 'decimal', precision: 12, scale: 2, name: 'balance_after', comment: '交易后余额' })
  balanceAfter: number;

  @Column({ type: 'varchar', length: 50, name: 'transaction_type', comment: '交易类型' })
  transactionType: string;

  @Column({ type: 'varchar', length: 50, name: 'business_type', comment: '业务类型：order_payment/refund/withdraw' })
  businessType: string;

  @Column({ type: 'bigint', unsigned: true, nullable: true, name: 'business_id', comment: '业务ID（订单ID/退款ID/提现ID等）' })
  businessId: number;

  @Column({ type: 'varchar', length: 32, nullable: true, name: 'business_no', comment: '业务单号' })
  businessNo: string;

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

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