import { ApiProperty, ApiPropertyOptional } from '@nestjs/swagger';
import { CobRefinanciacionEstado } from '@prisma/client';
import { Type } from 'class-transformer';
import {
  ArrayMinSize,
  IsArray,
  IsDateString,
  IsEnum,
  IsIn,
  IsInt,
  IsNumber,
  IsOptional,
  IsString,
  IsUUID,
  Max,
  Min,
  ValidateNested,
} from 'class-validator';

export type PoliticaInteres = 'CAPITALIZAR' | 'EXONERAR' | 'MANTENER';

export class SimularRefinanciacionDto {
  @ApiProperty()
  @IsUUID()
  cliente_id: string;

  @ApiProperty({ type: [String] })
  @IsArray()
  @ArrayMinSize(1)
  @IsUUID('all', { each: true })
  factura_cuota_ids: string[];

  @ApiProperty()
  @IsInt()
  @Min(1)
  @Max(120)
  cantidad_cuotas: number;

  @ApiProperty()
  @IsNumber()
  @Min(0)
  tasa_interes: number;

  @ApiProperty()
  @IsDateString()
  fecha_primera_cuota: string;

  @ApiProperty({ enum: ['CAPITALIZAR', 'EXONERAR', 'MANTENER'] })
  @IsIn(['CAPITALIZAR', 'EXONERAR', 'MANTENER'])
  politica_interes: PoliticaInteres;

  @ApiPropertyOptional({ default: 30 })
  @IsOptional()
  @IsInt()
  @Min(1)
  intervalo_dias?: number;
}

export class CreateRefinanciacionDto extends SimularRefinanciacionDto {
  @ApiProperty()
  @IsString()
  motivo: string;

  @ApiPropertyOptional()
  @IsOptional()
  @IsString()
  documento_url?: string;
}

export class EjecutarRefinanciacionDto {
  @ApiPropertyOptional()
  @IsOptional()
  @IsUUID()
  autorizado_por_id?: string;

  @ApiPropertyOptional()
  @IsOptional()
  @IsString()
  documento_url?: string;
}

export class AnularRefinanciacionDto {
  @ApiProperty()
  @IsString()
  motivo_anulacion: string;
}

export class QueryRefinanciacionesDto {
  @ApiPropertyOptional()
  @IsOptional()
  @IsUUID()
  cliente_id?: string;

  @ApiPropertyOptional({ enum: CobRefinanciacionEstado })
  @IsOptional()
  @IsEnum(CobRefinanciacionEstado)
  estado?: CobRefinanciacionEstado;

  @ApiPropertyOptional({ default: 1 })
  @IsOptional()
  @Type(() => Number)
  @IsInt()
  @Min(1)
  page?: number = 1;

  @ApiPropertyOptional({ default: 20 })
  @IsOptional()
  @Type(() => Number)
  @IsInt()
  @Min(1)
  limit?: number = 20;
}
