import { Entity, PrimaryGeneratedColumn, Column, CreateDateColumn, UpdateDateColumn, ManyToOne, JoinColumn } from 'typeorm';
import { User } from '../users/user.entity';
import { UserCard } from '../user-cards/user-card.entity';

@Entity('user_card_balance_records')
export class UserCardBalanceRecord {
  @PrimaryGeneratedColumn({ unsigned: true })
  id!: number;

  @Column({ name: 'user_id', type: 'int', unsigned: true, comment: '用户ID' })
  userId!: number;

  @Column({ name: 'card_no', type: 'varchar', length: 20, comment: '卡号' })
  cardNo!: string;

  @Column({ name: 'amount', type: 'decimal', precision: 10, scale: 2, unsigned: true, default: 0.00, comment: '变动金额' })
  amount!: number;

  @Column({ name: 'amount_before', type: 'decimal', precision: 10, scale: 2, unsigned: true, default: 0.00, comment: '变动前金额' })
  amountBefore!: number;

  @Column({ name: 'amount_after', type: 'decimal', precision: 10, scale: 2, unsigned: true, default: 0.00, comment: '变动后金额' })
  amountAfter!: number;

  @Column({ name: 'order_no', type: 'varchar', length: 32, comment: '订单号' })
  orderNo!: string;

  @Column({ name: 'type', type: 'int', comment: '类型 1消费 2退款' })
  type!: number;

  @Column({ name: 'remark', type: 'varchar', length: 32, nullable: true, comment: '备注' })
  remark!: string | null;

  @Column({ name: 'created_by', type: 'int', unsigned: true, nullable: true, comment: '创建人ID' })
  createdBy!: number | null;

  @Column({ name: 'updated_by', type: 'int', unsigned: true, nullable: true, comment: '更新人ID' })
  updatedBy!: number | null;

  @CreateDateColumn({ name: 'created_at', type: 'timestamp' })
  createdAt!: Date;

  @UpdateDateColumn({ name: 'updated_at', type: 'timestamp' })
  updatedAt!: Date;

  @ManyToOne(() => User)
  @JoinColumn({ name: 'user_id', referencedColumnName: 'id' })
  user!: User;

  @ManyToOne(() => UserCard)
  @JoinColumn({ name: 'card_no', referencedColumnName: 'cardNo' })
  userCard!: UserCard;
}