import {
  Entity,
  PrimaryGeneratedColumn,
  Column,
  CreateDateColumn,
  Index,
  ManyToOne,
  JoinColumn,
} from 'typeorm';
import { CfdiInvoice } from './cfdi-invoice.entity.js';

/**
 * CFDI operation type aligned with DDL fiscal.cfdi_operation_type
 */
export enum CfdiOperationType {
  CREATE = 'create',
  STAMP = 'stamp',
  STAMP_RETRY = 'stamp_retry',
  CANCEL_REQUEST = 'cancel_request',
  CANCEL_ACCEPT = 'cancel_accept',
  CANCEL_REJECT = 'cancel_reject',
  CANCEL_COMPLETE = 'cancel_complete',
  VALIDATE = 'validate',
  DOWNLOAD = 'download',
  EMAIL = 'email',
  ERROR = 'error',
}

/**
 * CFDI Operation Log Entity
 * Aligned with DDL: fiscal.cfdi_operation_logs
 */
@Entity({ schema: 'fiscal', name: 'cfdi_operation_logs' })
@Index('idx_cfdi_operation_logs_tenant', ['tenantId'])
@Index('idx_cfdi_operation_logs_cfdi', ['cfdiInvoiceId'])
@Index('idx_cfdi_operation_logs_operation', ['operationType'])
@Index('idx_cfdi_operation_logs_created', ['createdAt'])
export class CfdiLog {
  @PrimaryGeneratedColumn('uuid')
  id: string;

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

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

  @Column({ type: 'varchar', length: 36, nullable: true, name: 'cfdi_uuid' })
  cfdiUuid: string | null;

  @Column({
    type: 'enum',
    enum: CfdiOperationType,
    nullable: false,
    name: 'operation_type',
  })
  operationType: CfdiOperationType;

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

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

  @Column({ type: 'boolean', default: false, nullable: false, name: 'success' })
  success: boolean;

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

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

  @Column({ type: 'jsonb', nullable: true, name: 'error_details' })
  errorDetails: Record<string, any> | null;

  @Column({ type: 'jsonb', nullable: true, name: 'request_payload' })
  requestPayload: Record<string, any> | null;

  @Column({ type: 'jsonb', nullable: true, name: 'response_payload' })
  responsePayload: Record<string, any> | null;

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

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

  @Column({ type: 'inet', nullable: true, name: 'ip_address' })
  ipAddress: string | null;

  @Column({ type: 'varchar', length: 500, nullable: true, name: 'user_agent' })
  userAgent: string | null;

  @Column({ type: 'integer', nullable: true, name: 'duration_ms' })
  durationMs: number | null;

  // Relation
  @ManyToOne(() => CfdiInvoice, (invoice) => invoice.logs, { onDelete: 'SET NULL' })
  @JoinColumn({ name: 'cfdi_invoice_id' })
  cfdiInvoice: CfdiInvoice | null;

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

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