import { Transform, Type } from 'class-transformer';
import {
  ArrayMinSize,
  IsArray,
  IsBoolean,
  IsDateString,
  IsInt,
  IsOptional,
  IsString,
  IsUUID,
  Max,
  MaxLength,
  Min,
  ValidateNested,
} from 'class-validator';

export class UpdateLabCaseImportantDto {
  @IsBoolean()
  isImportant: boolean;
}

export class UpdateLabCaseExternalCodeDto {
  @IsOptional()
  @IsString()
  @MaxLength(64)
  externalCode?: string | null;
}

export class AssignLabCaseTaskDto {
  @IsOptional()
  @IsUUID()
  assigneeUserId?: string | null;
}

export class ListLabCasesDto {
  @IsOptional()
  @IsString()
  q?: string;

  @IsOptional()
  @IsUUID()
  clinicOrganizationId?: string;

  @IsOptional()
  @IsString()
  prosthesisTypeCode?: string;

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

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

  @IsOptional()
  @Transform(({ value }) => Number(value))
  @IsInt()
  @Min(1)
  page = 1;

  @IsOptional()
  @Transform(({ value }) => Number(value))
  @IsInt()
  @Min(1)
  @Max(100)
  limit = 20;
}

export class ToothSelectionGroupDto {
  @IsString()
  @MaxLength(64)
  groupId: string;

  @IsString()
  kind: string;

  @IsArray()
  @IsString({ each: true })
  teeth: string[];
}

export class LabInternalToothProsthesisDto {
  @IsString()
  @MaxLength(8)
  tooth: string;

  @IsString()
  @MaxLength(64)
  prosthesisTypeCode: string;

  @IsOptional()
  @IsString()
  @MaxLength(64)
  selectionGroupId?: string;
}

export class LabInternalCaseLineDto {
  @IsString()
  @MaxLength(64)
  clientId: string;

  @IsOptional()
  @IsUUID()
  id?: string;

  @IsArray()
  @IsString({ each: true })
  teeth: string[];

  @IsOptional()
  @IsArray()
  @ValidateNested({ each: true })
  @Type(() => ToothSelectionGroupDto)
  toothSelectionGroups?: ToothSelectionGroupDto[];

  @IsOptional()
  @IsString()
  @MaxLength(5000)
  comment?: string;

  @IsOptional()
  @IsArray()
  @ValidateNested({ each: true })
  @Type(() => LabInternalToothProsthesisDto)
  toothProsthesis?: LabInternalToothProsthesisDto[];

  @IsOptional()
  @IsArray()
  @IsUUID(undefined, { each: true })
  attachmentIds?: string[];
}

export class CreateLabInternalCaseDto {
  @IsOptional()
  @IsString()
  @MaxLength(200)
  referringClinicName?: string;

  @IsOptional()
  @IsString()
  @MaxLength(200)
  referringDentistName?: string;

  @IsOptional()
  @IsString()
  @MaxLength(200)
  patientDisplayName?: string;

  @IsOptional()
  @IsString()
  @MaxLength(32)
  patientDisplayMobile?: string;

  @IsOptional()
  @IsUUID()
  partnerClinicOrganizationId?: string | null;

  @IsOptional()
  @IsDateString()
  dueDate?: string | null;
}

export class UpdateLabInternalCaseDto extends CreateLabInternalCaseDto {
  @IsOptional()
  @IsArray()
  @ValidateNested({ each: true })
  @Type(() => LabInternalCaseLineDto)
  lines?: LabInternalCaseLineDto[];
}
