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

@Entity('accounts')
@Index(['account_type', 'owner_id'], { unique: true })
export class Account {
  @PrimaryGeneratedColumn({ type: 'bigint', unsigned: true })
  id: number;

  @Column({
    type: 'enum',
    enum: ['user', 'merchant', 'platform'],
    comment: '账户类型'
  })
  @Index()
  account_type: 'user' | 'merchant' | 'platform';

  @Column({ type: 'bigint', unsigned: true, comment: '所有者ID（user_id/merchant_id/platform固定为0）' })
  owner_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: '累计收入' })
  total_income: number;

  @Column({ type: 'decimal', precision: 12, scale: 2, default: 0, comment: '累计支出' })
  total_expense: 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;
}
