import {
  IsString,
  IsUUID,
  IsOptional,
  IsEnum,
  MaxLength,
  MinLength,
} from 'class-validator';
import { CancellationReason } from '../enums/cancellation-reason.enum.js';

/**
 * DTO para cancelar un CFDI
 */
export class CancelInvoiceDto {
  @IsUUID()
  tenantId: string;

  @IsUUID()
  cfdiInvoiceId: string;

  @IsEnum(CancellationReason)
  cancellationReason: CancellationReason;

  @IsUUID()
  @IsOptional()
  replacementUuid?: string;
}

/**
 * DTO para cancelar por UUID directamente
 */
export class CancelByUuidDto {
  @IsUUID()
  tenantId: string;

  @IsString()
  @MinLength(12)
  @MaxLength(13)
  rfcEmisor: string;

  @IsUUID()
  uuid: string;

  @IsEnum(CancellationReason)
  cancellationReason: CancellationReason;

  @IsUUID()
  @IsOptional()
  replacementUuid?: string;
}

/**
 * DTO de respuesta de cancelación
 */
export class CancelResponseDto {
  success: boolean;
  cancellationId: string;
  uuid: string;
  status: string;
  acuseXml?: string;
  cancellationDate?: Date;
  satStatus?: string;
  errorCode?: string;
  errorMessage?: string;
}

/**
 * DTO para responder a una solicitud de cancelación
 * (cuando el receptor debe aceptar/rechazar)
 */
export class CancellationAnswerDto {
  @IsUUID()
  tenantId: string;

  @IsString()
  @MinLength(12)
  @MaxLength(13)
  rfcReceptor: string;

  @IsUUID()
  uuid: string;

  @IsString()
  answer: 'accept' | 'reject';

  @IsString()
  @IsOptional()
  @MaxLength(500)
  rejectionReason?: string;
}

/**
 * DTO para listar cancelaciones pendientes
 */
export class PendingCancellationsFilterDto {
  @IsUUID()
  tenantId: string;

  @IsString()
  @MinLength(12)
  @MaxLength(13)
  rfc: string;
}

/**
 * DTO para filtrar CFDIs
 */
export class CfdiFilterDto {
  @IsUUID()
  @IsOptional()
  tenantId?: string;

  @IsUUID()
  @IsOptional()
  companyId?: string;

  @IsString()
  @IsOptional()
  rfcEmisor?: string;

  @IsString()
  @IsOptional()
  rfcReceptor?: string;

  @IsString()
  @IsOptional()
  status?: string;

  @IsString()
  @IsOptional()
  cfdiType?: string;

  @IsString()
  @IsOptional()
  uuid?: string;

  @IsString()
  @IsOptional()
  serie?: string;

  @IsString()
  @IsOptional()
  folio?: string;

  @IsString()
  @IsOptional()
  dateFrom?: string;

  @IsString()
  @IsOptional()
  dateTo?: string;

  @IsOptional()
  limit?: number;

  @IsOptional()
  offset?: number;
}
