import { Transform } from 'class-transformer';
import {
  IsEmail,
  IsEnum,
  IsOptional,
  IsString,
  IsUUID,
  MaxLength,
  MinLength,
  ValidateIf,
} from 'class-validator';

import {
  ClientStatus,
  PrimaryMetricType,
} from '../../../generated/prisma/enums';

export class CreateClientDto {
  @Transform(({ value }) => (typeof value === 'string' ? value.trim() : value))
  @ValidateIf((_, value) => value !== undefined && value !== '')
  @IsString()
  @MinLength(2)
  @MaxLength(120)
  companyName?: string;

  @Transform(({ value }) => (typeof value === 'string' ? value.trim() : value))
  @ValidateIf((_, value) => value !== undefined && value !== '')
  @IsString()
  @MinLength(2)
  @MaxLength(255)
  website?: string;

  @Transform(({ value }) => (typeof value === 'string' ? value.trim() : value))
  @IsString()
  @MinLength(2)
  @MaxLength(100)
  contactName!: string;

  @Transform(({ value }) => (typeof value === 'string' ? value.trim() : value))
  @IsEmail()
  @MaxLength(254)
  email!: string;

  @Transform(({ value }) => (typeof value === 'string' ? value.trim() : value))
  @ValidateIf((_, value) => value !== undefined && value !== '')
  @IsString()
  @MinLength(6)
  @MaxLength(32)
  phone?: string;

  @ValidateIf((_, value) => value !== undefined)
  @IsEnum(PrimaryMetricType)
  primaryMetricType?: PrimaryMetricType;

  @IsEnum(ClientStatus)
  status!: ClientStatus;

  @Transform(({ value }) => (typeof value === 'string' ? value.trim() : value))
  @IsUUID()
  assignedUserId!: string;

  @IsOptional()
  @Transform(({ value }) => {
    if (typeof value !== 'string') {
      return value;
    }

    const trimmed = value.trim();
    return trimmed.length === 0 ? null : trimmed;
  })
  @IsString()
  @MaxLength(2000)
  notes?: string | null;
}
