import { ApiProperty, ApiPropertyOptional } from '@nestjs/swagger';
import { Type } from 'class-transformer';
import {
  ArrayMaxSize,
  ArrayMinSize,
  IsArray,
  IsEmail,
  IsNumber,
  IsOptional,
  IsString,
  IsUUID,
  Max,
  MaxLength,
  Min,
  ValidateNested,
} from 'class-validator';
import { TramoDto } from '../../planes/dto/plan.dto';

export class OpcionCotizacionDto {
  @ApiPropertyOptional({ description: 'Plan del proyecto; o mandar los tramos de un plan a medida' })
  @IsOptional()
  @IsUUID()
  plan_id?: string;

  @ApiPropertyOptional({ type: [TramoDto] })
  @IsOptional()
  @IsArray()
  @ValidateNested({ each: true })
  @Type(() => TramoDto)
  tramos?: TramoDto[];

  @ApiPropertyOptional({ example: 'A medida' }) @IsOptional() @IsString() @MaxLength(100) nombre?: string;

  @ApiPropertyOptional({ example: 5, description: 'Descuento sobre el precio de lista, en %' })
  @IsOptional()
  @IsNumber({ maxDecimalPlaces: 2 })
  @Min(0, { message: 'El descuento no puede ser negativo' })
  @Max(100, { message: 'El descuento no puede pasar del 100 %' })
  descuento_pct?: number;
}

export class CreateCotizacionDto {
  @ApiProperty() @IsUUID() unidad_id: string;

  @ApiPropertyOptional({ description: 'Cliente del ERP; si todavía no lo es, cargar el prospecto' })
  @IsOptional()
  @IsUUID()
  cliente_id?: string;

  @ApiPropertyOptional({ example: 'María Benítez' })
  @IsOptional()
  @IsString()
  @MaxLength(150)
  prospecto_nombre?: string;
  @ApiPropertyOptional({ example: '0981 123 456' })
  @IsOptional()
  @IsString()
  @MaxLength(30)
  prospecto_telefono?: string;
  @ApiPropertyOptional({ example: 'maria@ejemplo.com' })
  @IsOptional()
  @IsEmail()
  @MaxLength(120)
  prospecto_email?: string;
  @ApiPropertyOptional() @IsOptional() @IsString() @MaxLength(2000) notas?: string;

  @ApiProperty({ type: [OpcionCotizacionDto], description: 'Hasta tres planes para comparar' })
  @IsArray()
  @ArrayMinSize(1, { message: 'La cotización necesita al menos un plan' })
  @ArrayMaxSize(3, { message: 'Se comparan hasta tres planes' })
  @ValidateNested({ each: true })
  @Type(() => OpcionCotizacionDto)
  opciones: OpcionCotizacionDto[];
}
