import { Transform } from 'class-transformer';
import { IsDateString, IsEmail, IsOptional, IsString, MaxLength, MinLength } from 'class-validator';
import { ErrorCode } from '../../../common/errors';

function emptyStringToUndefined({ value }: { value: unknown }): unknown {
  if (typeof value === 'string' && value.trim() === '') {
    return undefined;
  }
  return value;
}

export class CreatePatientDto {
  @IsString()
  @MinLength(1, { message: ErrorCode.VALIDATION_FIELD_REQUIRED })
  @MaxLength(80)
  firstName: string;

  @IsString()
  @MinLength(1, { message: ErrorCode.VALIDATION_FIELD_REQUIRED })
  @MaxLength(80)
  lastName: string;

  @IsString()
  @MinLength(1, { message: ErrorCode.VALIDATION_FIELD_REQUIRED })
  @MaxLength(30)
  mobile: string;

  @IsOptional()
  @Transform(emptyStringToUndefined)
  @IsEmail({}, { message: ErrorCode.VALIDATION_EMAIL_INVALID })
  email?: string;

  @IsOptional()
  @IsDateString()
  dateOfBirth?: string;

  @IsOptional()
  @IsString()
  @MaxLength(1000)
  notes?: string;
}
