import { ApiProperty, ApiPropertyOptional } from '@nestjs/swagger';
import { Type } from 'class-transformer';
import {
    IsArray,
    IsAlphanumeric,
    IsBoolean,
    IsNotEmpty,
    IsNumber,
    IsOptional,
    IsString,
    IsUUID,
    Length,
    MaxLength,
    ValidateNested,
} from 'class-validator';

export class ReciboCobroDetalleDto {
  @ApiProperty({ example: 'cd88ac6a-2b0e-41bc-afd9-24b594cb8daf' })
  @IsUUID()
  @IsNotEmpty({ message: 'La cuota de factura es obligatoria' })
  factura_cuota_id: string;

  @ApiProperty({ example: 'cd88ac6a-2b0e-41bc-afd9-24b594cb8daf' })
  @IsUUID()
  @IsNotEmpty({ message: 'El medio de pago es obligatorio' })
  medio_pago_id: string;

  @ApiPropertyOptional({
    example: 100000,
    description: 'Monto original de la cuota (antes de descuento)',
  })
  @IsNumber()
  @IsOptional()
  monto_original?: number;

  @ApiProperty({ example: 150000 })
  @IsNumber()
  @IsNotEmpty({ message: 'El monto pagado es obligatorio' })
  monto_pagado: number;

  @ApiPropertyOptional({
    example: 5000,
    description: 'Descuento aplicado a esta cuota',
  })
  @IsNumber()
  @IsOptional()
  descuento_aplicado?: number;

  @ApiPropertyOptional({
    example: false,
    description: 'Si es un pago parcial de la cuota',
  })
  @IsBoolean()
  @IsOptional()
  es_pago_parcial?: boolean;

  @ApiPropertyOptional({ example: 1500, description: 'Mora calculada para esta cuota' })
  @IsNumber()
  @IsOptional()
  mora_monto?: number;



  @ApiPropertyOptional({ example: 'cd88ac6a-2b0e-41bc-afd9-24b594cb8daf' })
  @IsUUID()
  @IsOptional()
  moneda_id?: string;

  @ApiPropertyOptional({ example: 7500 })
  @IsNumber()
  @IsOptional()
  cotizacion?: number;

  @ApiPropertyOptional({ example: 'cd88ac6a-2b0e-41bc-afd9-24b594cb8daf' })
  @IsUUID()
  @IsOptional()
  tarjeta_id?: string;

  @ApiPropertyOptional({ example: 'cd88ac6a-2b0e-41bc-afd9-24b594cb8daf' })
  @IsUUID()
  @IsOptional()
  forma_procesamiento_pago_id?: string;

  @ApiPropertyOptional({ example: '1234' })
  @IsString()
  @IsOptional()
  @MaxLength(4)
  numero_tarjeta?: string;

  @ApiPropertyOptional({ example: 'Juan Perez' })
  @IsString()
  @IsOptional()
  @MaxLength(60)
  titular_tarjeta?: string;

  @ApiPropertyOptional({ example: '123456' })
  @IsString()
  @IsOptional()
  @MaxLength(10)
  codigo_autorizacion?: string;

  @ApiPropertyOptional({ example: 'Bancard S.A.' })
  @IsString()
  @IsOptional()
  @MaxLength(60)
  razon_social_procesadora?: string;

  @ApiPropertyOptional({ example: '80012345' })
  @IsString()
  @IsOptional()
  @MaxLength(8)
  ruc_procesadora?: string;

  @ApiPropertyOptional({ example: 1 })
  @IsNumber()
  @IsOptional()
  dv_procesadora?: number;

  @ApiPropertyOptional({ example: '00012345', description: 'Número de cheque — 8 caracteres alfanuméricos (dNumCheq)' })
  @IsString()
  @IsOptional()
  @IsAlphanumeric()
  @Length(8, 8, { message: 'El número de cheque debe tener exactamente 8 caracteres' })
  numero_cheque?: string;

  @ApiPropertyOptional({ example: 'Bancop', description: 'Banco emisor del cheque — 4 a 20 caracteres (dBcoEmi)' })
  @IsString()
  @IsOptional()
  @Length(4, 20, { message: 'El banco emisor debe tener entre 4 y 20 caracteres' })
  banco_emisor?: string;

  @ApiPropertyOptional({ example: 'cd88ac6a-2b0e-41bc-afd9-24b594cb8daf' })
  @IsUUID()
  @IsOptional()
  banco_id?: string;
}

export class CreateReciboCobroDto {
  @ApiPropertyOptional({
    example: 40000,
    description:
      'Interés manual que este cobro cobra (de las cuotas cobradas y/o de su cuenta). ' +
      'Sin esto no se cobra nada de interés: un cobro que no lo incluye —la app del ' +
      'cobrador imputando cuotas— lo deja pendiente para el próximo.',
  })
  @IsNumber()
  @IsOptional()
  interes_manual_cobrado?: number;

  @ApiProperty({ example: 'cd88ac6a-2b0e-41bc-afd9-24b594cb8daf' })
  @IsUUID()
  @IsOptional()
  empresa_id?: string;

  @ApiProperty({ example: 'cd88ac6a-2b0e-41bc-afd9-24b594cb8daf' })
  @IsUUID()
  @IsNotEmpty({ message: 'La sucursal es obligatoria' })
  sucursal_id: string;

  @ApiPropertyOptional({ example: 'cd88ac6a-2b0e-41bc-afd9-24b594cb8daf' })
  @IsUUID()
  @IsOptional()
  sesion_caja_id?: string;

  @ApiProperty({ example: 'cd88ac6a-2b0e-41bc-afd9-24b594cb8daf' })
  @IsUUID()
  @IsNotEmpty({ message: 'El cliente es obligatorio' })
  cliente_id: string;

  @ApiPropertyOptional({ example: 'cd88ac6a-2b0e-41bc-afd9-24b594cb8daf' })
  @IsUUID()
  @IsOptional()
  usuario_id?: string;

  @ApiPropertyOptional({ example: 'cd88ac6a-2b0e-41bc-afd9-24b594cb8daf' })
  @IsUUID()
  @IsOptional()
  moneda_id?: string;

  @ApiPropertyOptional({ example: 7500 })
  @IsNumber()
  @IsOptional()
  cotizacion?: number;

  @ApiPropertyOptional({
    example: 150000,
    description: 'Monto que realmente paga el cliente',
  })
  @IsNumber()
  @IsOptional()
  monto_recibido?: number;

  @ApiPropertyOptional({
    example: 10,
    description: 'Porcentaje de descuento aplicado',
  })
  @IsNumber()
  @IsOptional()
  descuento_porcentaje?: number;

  @ApiPropertyOptional({
    example: 50000,
    description: 'Monto fijo de descuento',
  })
  @IsNumber()
  @IsOptional()
  descuento_monto?: number;

  @ApiPropertyOptional({ example: 'Descuento por pago anticipado' })
  @IsString()
  @IsOptional()
  @MaxLength(500)
  motivo_descuento?: string;

  @ApiPropertyOptional({ example: 'Pago parcial de facturas pendientes' })
  @IsString()
  @IsOptional()
  @MaxLength(500)
  observacion?: string;

  @ApiPropertyOptional({ example: 3000, description: 'Total de mora cobrada en este recibo' })
  @IsNumber()
  @IsOptional()
  mora_total?: number;

  @ApiPropertyOptional({ example: 'cd88ac6a-2b0e-41bc-afd9-24b594cb8daf', description: 'Cobrador manual (sobreescribe auto-detección)' })
  @IsUUID()
  @IsOptional()
  cobrador_id?: string;

  @ApiPropertyOptional({ example: '2026-03-15', description: 'Fecha del comprobante de cobro (YYYY-MM-DD), para cobros externos' })
  @IsString()
  @IsOptional()
  fecha_registro?: string;

  @ApiPropertyOptional({ example: '2026-03-15', description: 'Fecha de emisión del recibo (YYYY-MM-DD). Si no se envía, se usa la fecha actual.' })
  @IsString()
  @IsOptional()
  fecha_emision?: string;

  @ApiProperty({ type: [ReciboCobroDetalleDto] })
  @IsArray()
  @ValidateNested({ each: true })
  @Type(() => ReciboCobroDetalleDto)
  detalles: ReciboCobroDetalleDto[];
}

export class PreviewDistribucionDto {
  @ApiProperty({ type: [String], description: 'IDs de las cuotas a pagar' })
  @IsArray()
  @IsUUID('4', { each: true })
  cuota_ids: string[];

  @ApiProperty({
    example: 150000,
    description: 'Monto total que el cliente quiere pagar',
  })
  @IsNumber()
  @IsNotEmpty()
  monto_recibido: number;

  @ApiPropertyOptional({
    example: 'cd88ac6a-2b0e-41bc-afd9-24b594cb8daf',
    description: 'Solo se usa cuando el preview alimenta la creación de un recibo. Omitir cuando se llama únicamente para calcular mora estimada.',
  })
  @IsUUID()
  @IsOptional()
  medio_pago_id?: string;

  @ApiPropertyOptional({
    example: 40000,
    deprecated: true,
    description:
      'DEPRECADO — se ignora. El interés manual se imputa automáticamente después ' +
      'del capital, con lo que sobra del monto entregado. Se mantiene para no ' +
      'rechazar cobros de clientes viejos que todavía lo mandan.',
  })
  @IsNumber()
  @IsOptional()
  interes_manual_monto?: number;

  @ApiPropertyOptional({ example: 10, description: 'Porcentaje de descuento' })
  @IsNumber()
  @IsOptional()
  descuento_porcentaje?: number;

  @ApiPropertyOptional({
    example: 50000,
    description: 'Monto fijo de descuento',
  })
  @IsNumber()
  @IsOptional()
  descuento_monto?: number;

  @ApiPropertyOptional({ example: 'Descuento por pago anticipado' })
  @IsString()
  @IsOptional()
  @MaxLength(500)
  motivo_descuento?: string;

  @ApiPropertyOptional({ example: 'cd88ac6a-2b0e-41bc-afd9-24b594cb8daf' })
  @IsUUID()
  @IsOptional()
  moneda_id?: string;

  @ApiPropertyOptional({ example: 7500 })
  @IsNumber()
  @IsOptional()
  cotizacion?: number;
}
