import {
  IsArray,
  IsBoolean,
  IsDateString,
  IsIn,
  IsOptional,
  IsString,
  IsUUID,
  MaxLength,
  ValidateNested,
} from 'class-validator';
import { Type } from 'class-transformer';

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

  @IsIn(['connected', 'single'])
  kind: 'connected' | 'single';

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

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

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

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

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

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

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

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

export class SaveTreatmentDraftDto {
  @IsArray()
  @ValidateNested({ each: true })
  @Type(() => SaveTreatmentDetailDto)
  details: SaveTreatmentDetailDto[];
}

export class LabCaseToothProsthesisDto {
  @IsUUID()
  treatmentDetailId: string;

  @IsString()
  @MaxLength(8)
  tooth: string;

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

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

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

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

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

  @IsUUID()
  treatmentDetailId: string;

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

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

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

export class UpdateLabCaseDueDateDto {
  @IsOptional()
  @IsDateString()
  dueDate?: string | null;
}

export class SaveTreatmentLabCasesDto {
  @IsArray()
  @ValidateNested({ each: true })
  @Type(() => SaveLabCaseDto)
  labCases: SaveLabCaseDto[];
}

export class ListPatientTreatmentHistoryDto {
  @IsOptional()
  limit?: number;
}

export class CreateStandaloneTreatmentDto {
  @IsOptional()
  @IsUUID()
  patientId?: string;

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

  @IsDateString()
  treatmentAt: string;
}

export class ListDayTreatmentsDto {
  @IsDateString()
  from: string;

  @IsDateString()
  to: string;
}
