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

/**
 * Payment complement status aligned with DDL fiscal.payment_complement_status
 */
export enum PaymentComplementStatus {
  DRAFT = 'draft',
  PENDING = 'pending',
  PROCESSING = 'processing',
  STAMPED = 'stamped',
  ERROR = 'error',
  CANCELLED = 'cancelled',
  CANCELLATION_PENDING = 'cancellation_pending',
}

/**
 * CFDI Payment Complement Entity
 * Aligned with DDL: fiscal.cfdi_payment_complements
 */
@Entity({ schema: 'fiscal', name: 'cfdi_payment_complements' })
@Index('idx_cfdi_payment_complements_tenant', ['tenantId'])
@Index('idx_cfdi_payment_complements_uuid', ['uuid'], { unique: true, where: 'uuid IS NOT NULL' })
@Index('idx_cfdi_payment_complements_payment', ['paymentId'])
@Index('idx_cfdi_payment_complements_status', ['status'])
@Index('idx_cfdi_payment_complements_created', ['createdAt'])
export class CfdiPaymentComplement {
  @PrimaryGeneratedColumn('uuid')
  id: string;

  @Column({ type: 'uuid', nullable: false, name: 'tenant_id' })
  tenantId: string;

  @Column({ type: 'uuid', nullable: true, name: 'payment_id' })
  paymentId: string | null;

  @Column({ type: 'uuid', nullable: true, name: 'certificate_id' })
  certificateId: string | null;

  // CFDI Identity
  @Column({ type: 'varchar', length: 36, nullable: true })
  uuid: string | null;

  @Column({ type: 'varchar', length: 25, nullable: true })
  serie: string | null;

  @Column({ type: 'varchar', length: 40, nullable: true })
  folio: string | null;

  // Issuer (Emisor)
  @Column({ type: 'varchar', length: 13, nullable: false, name: 'issuer_rfc' })
  issuerRfc: string;

  @Column({ type: 'varchar', length: 300, nullable: false, name: 'issuer_name' })
  issuerName: string;

  @Column({ type: 'varchar', length: 10, nullable: false, name: 'issuer_fiscal_regime' })
  issuerFiscalRegime: string;

  // Receiver (Receptor)
  @Column({ type: 'varchar', length: 13, nullable: false, name: 'receiver_rfc' })
  receiverRfc: string;

  @Column({ type: 'varchar', length: 300, nullable: false, name: 'receiver_name' })
  receiverName: string;

  @Column({ type: 'varchar', length: 10, nullable: true, name: 'receiver_fiscal_regime' })
  receiverFiscalRegime: string | null;

  @Column({ type: 'varchar', length: 5, nullable: true, name: 'receiver_zip_code' })
  receiverZipCode: string | null;

  // Expedition place
  @Column({ type: 'varchar', length: 5, nullable: false, name: 'expedition_place' })
  expeditionPlace: string;

  // Payment details
  @Column({ type: 'date', nullable: false, name: 'payment_date' })
  paymentDate: Date;

  @Column({ type: 'varchar', length: 10, nullable: false, name: 'payment_form' })
  paymentForm: string;

  @Column({ type: 'varchar', length: 3, nullable: false, name: 'currency' })
  currency: string;

  @Column({ type: 'decimal', precision: 10, scale: 6, nullable: true, name: 'exchange_rate' })
  exchangeRate: number | null;

  @Column({ type: 'decimal', precision: 18, scale: 6, nullable: false, name: 'total_amount' })
  totalAmount: number;

  @Column({ type: 'varchar', length: 100, nullable: true, name: 'operation_number' })
  operationNumber: string | null;

  // Bank info
  @Column({ type: 'varchar', length: 13, nullable: true, name: 'payer_bank_rfc' })
  payerBankRfc: string | null;

  @Column({ type: 'varchar', length: 300, nullable: true, name: 'payer_bank_name' })
  payerBankName: string | null;

  @Column({ type: 'varchar', length: 50, nullable: true, name: 'payer_account' })
  payerAccount: string | null;

  @Column({ type: 'varchar', length: 13, nullable: true, name: 'beneficiary_bank_rfc' })
  beneficiaryBankRfc: string | null;

  @Column({ type: 'varchar', length: 50, nullable: true, name: 'beneficiary_account' })
  beneficiaryAccount: string | null;

  // Certificate
  @Column({ type: 'varchar', length: 20, nullable: true, name: 'certificate_number' })
  certificateNumber: string | null;

  // Status
  @Column({
    type: 'enum',
    enum: PaymentComplementStatus,
    default: PaymentComplementStatus.DRAFT,
    nullable: false,
  })
  status: PaymentComplementStatus;

  @Column({ type: 'text', nullable: true, name: 'error_message' })
  errorMessage: string | null;

  @Column({ type: 'varchar', length: 50, nullable: true, name: 'error_code' })
  errorCode: string | null;

  // Stamps
  @Column({ type: 'timestamptz', nullable: true, name: 'stamp_date' })
  stampDate: Date | null;

  @Column({ type: 'text', nullable: true, name: 'stamp_cfdi_seal' })
  stampCfdiSeal: string | null;

  @Column({ type: 'text', nullable: true, name: 'stamp_sat_seal' })
  stampSatSeal: string | null;

  @Column({ type: 'varchar', length: 20, nullable: true, name: 'sat_certificate_number' })
  satCertificateNumber: string | null;

  @Column({ type: 'text', nullable: true, name: 'stamp_original_chain' })
  stampOriginalChain: string | null;

  // XML Storage
  @Column({ type: 'text', nullable: true, name: 'xml_original' })
  xmlOriginal: string | null;

  @Column({ type: 'text', nullable: true, name: 'xml_stamped' })
  xmlStamped: string | null;

  // Cancellation reference
  @Column({ type: 'uuid', nullable: true, name: 'cancellation_request_id' })
  cancellationRequestId: string | null;

  // Audit fields
  @Column({ type: 'timestamptz', nullable: true, name: 'stamped_at' })
  stampedAt: Date | null;

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

  @Column({ type: 'uuid', nullable: true, name: 'created_by' })
  createdBy: string | null;

  @UpdateDateColumn({ name: 'updated_at', type: 'timestamptz', nullable: true })
  updatedAt: Date | null;

  @Column({ type: 'uuid', nullable: true, name: 'updated_by' })
  updatedBy: string | null;

  @DeleteDateColumn({ name: 'deleted_at', type: 'timestamptz', nullable: true })
  deletedAt: Date | null;
}
