// ============================================
// FIEL (e.firma) Types
// ============================================

export interface FielUploadRequest {
  cerFile: string;  // Base64
  keyFile: string;  // Base64
  password: string;
}

export interface FielStatus {
  configured: boolean;
  rfc?: string;
  serialNumber?: string;
  validFrom?: string;
  validUntil?: string;
  isExpired?: boolean;
  daysUntilExpiration?: number;
}

// ============================================
// SAT Sync Types
// ============================================

export type SatSyncType = 'initial' | 'daily' | 'incremental';
export type SatSyncStatus = 'pending' | 'running' | 'completed' | 'failed';
export type CfdiSyncType = 'emitidos' | 'recibidos';

export interface SatSyncJob {
  id: string;
  tenantId: string;
  type: SatSyncType;
  status: SatSyncStatus;
  dateFrom: string;
  dateTo: string;
  cfdiType?: CfdiSyncType;
  satRequestId?: string;
  satPackageIds: string[];
  cfdisFound: number;
  cfdisDownloaded: number;
  cfdisInserted: number;
  cfdisUpdated: number;
  progressPercent: number;
  errorMessage?: string;
  startedAt?: string;
  completedAt?: string;
  createdAt: string;
  retryCount: number;
}

export interface SatSyncStatusResponse {
  hasActiveSync: boolean;
  currentJob?: SatSyncJob;
  lastCompletedJob?: SatSyncJob;
  lastCompletedInitialJob?: SatSyncJob;
  totalCfdisSynced: number;
}

export interface SatSyncHistoryResponse {
  jobs: SatSyncJob[];
  total: number;
  page: number;
  limit: number;
}

export interface StartSyncRequest {
  type?: SatSyncType;
  dateFrom?: string;
  dateTo?: string;
}

export interface StartSyncResponse {
  jobId: string;
  message: string;
}

// ============================================
// SAT Web Service Types
// ============================================

export interface SatAuthResponse {
  token: string;
  expiresAt: Date;
}

export interface SatDownloadRequest {
  rfcSolicitante: string;
  fechaInicio: Date;
  fechaFin: Date;
  tipoSolicitud: 'CFDI' | 'Metadata';
  tipoComprobante?: 'I' | 'E' | 'T' | 'N' | 'P';
  rfcEmisor?: string;
  rfcReceptor?: string;
}

export interface SatDownloadRequestResponse {
  idSolicitud: string;
  codEstatus: string;
  mensaje: string;
}

export interface SatVerifyResponse {
  codEstatus: string;
  estadoSolicitud: number;  // 1=Aceptada, 2=EnProceso, 3=Terminada, 4=Error, 5=Rechazada, 6=Vencida
  codigoEstadoSolicitud: string;
  numeroCfdis: number;
  mensaje: string;
  paquetes: string[];
}

export interface SatPackageResponse {
  paquete: string;  // Base64 ZIP
}

// ============================================
// SAT Error Codes
// ============================================

export const SAT_STATUS_CODES: Record<string, string> = {
  '5000': 'Solicitud recibida con éxito',
  '5002': 'Se agotó el límite de solicitudes',
  '5004': 'No se encontraron CFDIs',
  '5005': 'Solicitud duplicada',
};

export const SAT_REQUEST_STATUS: Record<number, string> = {
  1: 'Aceptada',
  2: 'En proceso',
  3: 'Terminada',
  4: 'Error',
  5: 'Rechazada',
  6: 'Vencida',
};
