import { Entity, ManyToOne, Property, Enum, Index } from '@mikro-orm/core';
import { BaseEntity } from '../../../common/entities/base.entity';
import { Order } from '../../orders/entities/order.entity';
import { PaymentGatewayEnum, PaymentMethodEnum, PaymentStatusEnum } from '../interface/payment';

@Entity({ tableName: 'payments' })
@Index({ properties: ['order'] })
export class Payment extends BaseEntity {
  @ManyToOne(() => Order)
  @Index()
  order!: Order;

  @Property({ type: 'decimal', precision: 10, scale: 0 })
  amount!: number;

  @Property({ unique: true, nullable: true })
  referenceId?: string | null;

  @Enum(() => PaymentMethodEnum)
  method!: PaymentMethodEnum;

  @Enum(() => PaymentGatewayEnum)
  gateway?: PaymentGatewayEnum | null = null;

  @Property({ nullable: true })
  transactionId?: string | null = null;

  @Enum(() => PaymentStatusEnum)
  status!: PaymentStatusEnum;

  @Property({ nullable: true })
  cardPan?: string | null = null;

  @Property({ type: 'json', nullable: true })
  verifyResponse?: Record<string, any> | null = null;

  @Property({ nullable: true })
  paidAt?: Date | null = null;

  @Property({ nullable: true })
  failedAt?: Date | null = null;

  @Property({ nullable: true })
  description?: string | null = null;
}
