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

@Entity('platform_transactions')
export class PlatformTransaction {
  @PrimaryGeneratedColumn({ type: 'bigint', unsigned: true })
  id: number;

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

  @Column({ type: 'bigint', unsigned: true, comment: '平台账户ID' })
  @Index()
  account_id: 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, comment: '交易前余额' })
  balance_before: number;

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

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

  @Column({ type: 'varchar', length: 50, comment: '业务类型：order/settlement/refund/service_fee' })
  @Index()
  business_type: string;

  @Column({ type: 'bigint', unsigned: true, nullable: true, comment: '业务ID（订单ID/结算单ID等）' })
  @Index()
  business_id: number;

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

  @Column({
    type: 'enum',
    enum: ['user', 'merchant'],
    nullable: true,
    comment: '对方账户类型'
  })
  related_account_type: 'user' | 'merchant';

  @Column({ type: 'bigint', unsigned: true, nullable: true, comment: '对方账户ID' })
  related_account_id: number;

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

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