import { Type } from 'class-transformer';
import { IsBoolean, IsDateString, IsIn, IsInt, IsNumber, IsOptional, IsString, IsUUID, Max, MaxLength, Min, ValidateNested } from 'class-validator';
import { Trim, TrimOrNull } from '../../../common/dto/field-transforms';
import { CONTACT_LIFECYCLES } from '../../contacts/contact.constants';
import { ENGAGEMENT_MODELS } from '../../deals/deal.constants';
import { CreateCustomerDto as CreateAccountDto } from '../../customers/dto/create-customer.dto';

export class ConvertLeadDto {
  @IsOptional()
  @IsUUID('all', { message: 'Choose a valid account' })
  accountId?: string;

  /** Customer details reviewed in the convert form (same fields and rules as New customer); lead values fill any gaps. */
  @IsOptional()
  @ValidateNested()
  @Type(() => CreateAccountDto)
  customer?: CreateAccountDto;

  /** true creates a new customer even when one with the same name exists. */
  @IsOptional()
  @IsBoolean()
  newAccount?: boolean;

  @IsOptional()
  @IsIn(CONTACT_LIFECYCLES)
  lifecycle?: (typeof CONTACT_LIFECYCLES)[number];

  @IsOptional()
  @IsBoolean()
  createDeal?: boolean;

  @TrimOrNull()
  @IsOptional()
  @IsString()
  @MaxLength(255)
  dealTitle?: string | null;

  @IsOptional()
  @Type(() => Number)
  @IsNumber({ maxDecimalPlaces: 2 })
  @Min(0)
  @Max(100000000000)
  dealAmount?: number;

  @IsOptional()
  @IsDateString({}, { message: 'Choose a valid expected close date' })
  closeDate?: string | null;

  @Trim()
  @IsOptional()
  @IsIn(ENGAGEMENT_MODELS)
  engagementModel?: (typeof ENGAGEMENT_MODELS)[number] | null;

  @IsOptional()
  @Type(() => Number)
  @IsInt()
  @Min(0)
  @Max(100000)
  estimatedHours?: number | null;
}
