/** Permanent dentition FDI ids (11–48), stored as two-digit strings */
export type FdiToothId =
  | '11'
  | '12'
  | '13'
  | '14'
  | '15'
  | '16'
  | '17'
  | '18'
  | '21'
  | '22'
  | '23'
  | '24'
  | '25'
  | '26'
  | '27'
  | '28'
  | '31'
  | '32'
  | '33'
  | '34'
  | '35'
  | '36'
  | '37'
  | '38'
  | '41'
  | '42'
  | '43'
  | '44'
  | '45'
  | '46'
  | '47'
  | '48';

export interface TreatmentAppointment {
  id: string;
  patientId: string;
  patientFirstName: string;
  patientLastName: string;
  patientMobile?: string | null;
  providerUserId: string;
  startAt: string;
  endAt: string;
  purpose: string;
}

export interface TreatmentAttachmentMeta {
  id: string;
  fileName: string;
  mimeType: string;
  sizeBytes: number;
}

export type TreatmentType = string;

export interface LabCaseToothProsthesisDraft {
  /** Treatment detail client id in the UI; mapped to UUID when saving. */
  detailClientId: string;
  tooth: string;
  prosthesisTypeCode: string;
  /** Links connected/single selection group for shared prosthesis + lab tasks. */
  selectionGroupId?: string;
}

export type ToothSelectionKind = 'connected' | 'single';

export interface ToothSelectionGroup {
  groupId: string;
  kind: ToothSelectionKind;
  teeth: FdiToothId[];
}

export interface LabCaseSendInfo {
  organizationId: string;
  organizationName: string;
  sentAt: string;
}

/** @deprecated Use LabCaseSendInfo */
export type TreatmentCaseSendInfo = LabCaseSendInfo;

export interface LabCaseTaskProgress {
  completed: number;
  total: number;
}

export interface PastTreatmentDetail {
  id: string;
  clientId: string;
  treatmentType: TreatmentType;
  teeth: FdiToothId[];
  toothSelectionGroups?: ToothSelectionGroup[];
  notes?: string | null;
  attachmentMetas?: TreatmentAttachmentMeta[];
  labCaseId?: string | null;
  destinationOrganizationId?: string | null;
  taskProgress?: LabCaseTaskProgress | null;
  sends?: LabCaseSendInfo[];
  sentAt?: string | null;
}

/** @deprecated Use PastTreatmentDetail */
export type PastTreatmentCase = PastTreatmentDetail;

export interface PastLabCase {
  id: string;
  clientId: string;
  destinationOrganizationId: string | null;
  sentAt?: string | null;
  treatmentDetailId: string | null;
  detail: {
    id: string;
    clientId: string;
    treatmentType: string;
    teeth: FdiToothId[];
    toothSelectionGroups?: ToothSelectionGroup[];
  } | null;
  toothProsthesis?: Array<{
    treatmentDetailId?: string | null;
    tooth: string;
    prosthesisTypeCode: string;
    selectionGroupId?: string;
  }>;
  sends?: LabCaseSendInfo[];
  dueDate?: string | null;
  taskProgress?: LabCaseTaskProgress | null;
  attachments?: TreatmentAttachmentMeta[];
  /** Present on sent cases — same share URL the lab Cases QR encodes. */
  shareUrl?: string | null;
}

export interface PastTreatment {
  id: string;
  patientId: string;
  appointmentId?: string | null;
  providerUserId?: string | null;
  title: string;
  treatmentAt: string;
  patient?: {
    id: string;
    firstName: string;
    lastName: string;
    isWalkIn: boolean;
    mobile?: string | null;
    email?: string | null;
  } | null;
  details: PastTreatmentDetail[];
  labCases: PastLabCase[];
  documents: TreatmentAttachmentMeta[];
}

export interface LinkedOrganizationOption {
  id: string;
  name: string;
  active: boolean;
}

export interface TreatmentDetailDraft {
  clientId: string;
  id?: string;
  treatmentType: TreatmentType;
  teeth: FdiToothId[];
  toothSelectionGroups: ToothSelectionGroup[];
  comment: string;
  attachmentMetas: TreatmentAttachmentMeta[];
  labCaseId?: string | null;
  taskProgress?: LabCaseTaskProgress | null;
  sendToOrganizationIds: string[];
  sends?: LabCaseSendInfo[];
  sentAt?: string | null;
}

/** @deprecated Use TreatmentDetailDraft — kept for editor components until Phase 4 rename */
export type TreatmentCaseDraft = TreatmentDetailDraft;

export interface LabCaseDraft {
  clientId: string;
  id?: string;
  destinationOrganizationId: string | null;
  detailClientId: string | null;
  toothProsthesis: LabCaseToothProsthesisDraft[];
  attachmentIds: string[];
  sentAt?: string | null;
  sends?: LabCaseSendInfo[];
  dueDate?: string | null;
  taskProgress?: LabCaseTaskProgress | null;
  shareUrl?: string | null;
}

export type SavedTreatmentDetailPayload = {
  clientId: string;
  id?: string;
  treatmentType: TreatmentType;
  teeth: FdiToothId[];
  toothSelectionGroups?: ToothSelectionGroup[];
  comment: string;
  attachmentIds: string[];
};

/** @deprecated Use SavedTreatmentDetailPayload */
export type SavedTreatmentCasePayload = SavedTreatmentDetailPayload;

export interface SaveLabCasePayload {
  clientId: string;
  id?: string;
  destinationOrganizationId?: string;
  treatmentDetailId: string;
  toothProsthesis?: Array<{
    treatmentDetailId: string;
    tooth: string;
    prosthesisTypeCode: string;
    selectionGroupId?: string;
  }>;
  attachmentIds?: string[];
  dueDate?: string | null;
}

export interface SaveTreatmentPayload {
  appointmentId: string;
  patientId: string;
  details: SavedTreatmentDetailPayload[];
}

export interface LabCaseResponse {
  id: string;
  clientId: string;
  destinationOrganizationId: string | null;
  sentAt: string | null;
  treatmentDetailId: string | null;
  detail: {
    id: string;
    clientId: string;
    treatmentType: string;
    teeth: string[];
    toothSelectionGroups?: ToothSelectionGroup[];
  } | null;
  sends: LabCaseSendInfo[];
  dueDate: string | null;
  taskProgress?: LabCaseTaskProgress | null;
  toothProsthesis?: LabCaseToothProsthesisDraft[];
  attachments?: TreatmentAttachmentMeta[];
  shareUrl?: string | null;
}
