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

@Entity('wallet_transactions')
@Index('idx_wallet_transactions_user_id', ['user_id'])
@Index('idx_wallet_transactions_reference', ['reference_type', 'reference_id'])
export class WalletTransactions {
  @PrimaryGeneratedColumn({
    type: 'bigint',
    comment: '主键ID',
  })
  id: bigint;

  @Column({
    type: 'bigint',
    nullable: false,
    comment: '关联users.id',
  })
  user_id: bigint;

  @Column({
    type: 'varchar',
    length: 24,
    nullable: false,
    comment: '流水类型：grant/spend/earn/refund',
  })
  type: string;

  @Column({
    type: 'int',
    nullable: false,
    comment: '变动数量，收入为正，支出为负',
  })
  amount: number;

  @Column({
    type: 'int',
    nullable: false,
    comment: '变动后余额',
  })
  balance_after: number;

  @Column({
    type: 'varchar',
    length: 50,
    nullable: false,
    comment: '业务引用类型',
  })
  reference_type: string;

  @Column({
    type: 'varchar',
    length: 100,
    nullable: false,
    comment: '业务引用ID',
  })
  reference_id: string;

  @Column({
    type: 'varchar',
    length: 255,
    nullable: true,
    comment: '流水备注',
  })
  note?: string | null;

  @Column({
    type: 'timestamp',
    default: () => 'CURRENT_TIMESTAMP',
    comment: '创建时间',
  })
  created_at: Date;
}
