import { Entity, PrimaryGeneratedColumn, Column, ManyToOne, JoinColumn, CreateDateColumn, UpdateDateColumn } from 'typeorm';
import { UserEntity } from '@/server/modules/users/user.entity';
import { PaymentStatus, PaymentType } from './payment.schema';

@Entity('payments')
export class PaymentEntity {
  @PrimaryGeneratedColumn({ unsigned: true, comment: '支付记录ID' })
  id!: number;

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

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

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

  @Column({ name: 'amount', type: 'decimal', precision: 10, scale: 2, comment: '支付金额' })
  amount!: number;

  @Column({ name: 'document_count', type: 'int', comment: '文档处理次数' })
  documentCount!: number;

  @Column({ name: 'payment_type', type: 'enum', enum: PaymentType, comment: '支付类型: recharge, consume, refund' })
  paymentType!: PaymentType;

  @Column({ name: 'status', type: 'int', default: PaymentStatus.PENDING, comment: '支付状态: 0-待支付,1-已完成,2-失败,3-已取消' })
  status!: PaymentStatus;

  @Column({ name: 'payment_method', type: 'varchar', length: 20, nullable: true, comment: '支付方式: alipay, wechat' })
  paymentMethod!: string | null;

  @Column({ name: 'transaction_id', type: 'varchar', length: 64, nullable: true, comment: '第三方交易ID' })
  transactionId!: string | null;

  @Column({ name: 'description', type: 'text', nullable: true, comment: '支付描述' })
  description!: string | null;

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

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

  constructor(partial?: Partial<PaymentEntity>) {
    Object.assign(this, partial);
  }
}