export type LabTaskStatus = 'IN_PROGRESS' | 'COMPLETED';

export interface LabCaseProsthesisGroup {
  prosthesisTypeCode: string;
  teeth: string[];
  connected?: boolean;
  selectionGroupId?: string;
}

export interface LabCaseListItem {
  id: string;
  sentAt: string | null;
  startedAt?: string | null;
  origin?: 'CLINIC_DISPATCH' | 'LAB_INTERNAL';
  dueDate: string | null;
  isOverdue: boolean;
  isImportant: boolean;
  hasUnread: boolean;
  clinic: { id: string; name: string };
  patient: {
    id: string;
    firstName: string;
    lastName: string;
    mobile: string;
  };
  prosthesisGroups: LabCaseProsthesisGroup[];
  taskProgress: { completed: number; total: number };
}

export interface LabTaskUser {
  id: string;
  name: string;
}

export interface LabTaskTimelineEvent {
  id: string;
  fromStatus: LabTaskStatus | null;
  toStatus: LabTaskStatus;
  changedAt: string;
  changedBy: LabTaskUser | null;
}

export interface LabCaseTask {
  id: string;
  treatmentDetailId: string;
  teeth: string[];
  treatmentType: string;
  prosthesisTypeCode: string;
  prosthesisTypeLabel: string;
  workflowStepCode?: string;
  stepOrder: number;
  stepLabel: string;
  status: LabTaskStatus;
  assignee: LabTaskUser | null;
  assignedAt: string | null;
  createdAt: string;
  lastStatusChangedAt: string | null;
  lastStatusChangedBy: LabTaskUser | null;
  timeline: LabTaskTimelineEvent[];
}

export interface LabCaseTaskGroup {
  treatmentDetailId: string;
  teeth: string[];
  treatmentType: string;
  prosthesisTypeCode: string;
  prosthesisTypeLabel: string;
  selectionGroupId?: string;
  connected?: boolean;
  tasks: LabCaseTask[];
}

export interface LabCaseComment {
  id: string;
  body: string;
  authorSide: 'LAB' | 'CLINIC';
  authorName: string | null;
  authorOrganizationName: string | null;
  visibleToClinic: boolean;
  createdAt: string;
  canToggleVisibility: boolean;
  showVisibilityStatus?: boolean;
}

export interface LabCaseAttachmentMeta {
  id: string;
  fileName: string;
  mimeType: string;
  sizeBytes: number;
  createdAt: string;
  detailClientKey?: string | null;
}

export interface LabCaseLine {
  id: string;
  clientId: string;
  treatmentType: string;
  teeth: string[];
  toothSelectionGroups?: unknown;
  comment: string | null;
}

export interface LabCaseDetail {
  id: string;
  sentAt: string | null;
  startedAt?: string | null;
  origin?: 'CLINIC_DISPATCH' | 'LAB_INTERNAL';
  dueDate: string | null;
  isOverdue: boolean;
  isImportant: boolean;
  /** Optional external lab-app code (e.g. exocad); used as PDF order number when set. */
  externalCode?: string | null;
  referringClinicName?: string | null;
  referringDentistName?: string | null;
  patientDisplayName?: string | null;
  patientDisplayMobile?: string | null;
  partnerClinicOrganizationId?: string | null;
  clinic: { id: string; name: string };
  patient: {
    id: string;
    firstName: string;
    lastName: string;
    mobile: string;
  };
  appointmentStartAt: string | null;
  treatmentType: string | null;
  detail: {
    id: string;
    treatmentType: string;
    teeth: string[];
    comment: string | null;
  } | null;
  lines?: LabCaseLine[];
  toothProsthesis: Array<{
    treatmentDetailId: string | null;
    lineId?: string | null;
    tooth: string;
    prosthesisTypeCode: string;
    selectionGroupId?: string;
  }>;
  attachments: LabCaseAttachmentMeta[];
  sends: Array<{
    organizationId: string;
    organizationName: string;
    sentAt: string;
  }>;
  tasks: LabCaseTask[];
  tasksByTooth: LabCaseTaskGroup[];
  taskProgress: { completed: number; total: number };
  shareUrl?: string | null;
}

export interface ListLabCasesParams {
  q?: string;
  page?: number;
  limit?: number;
  clinicOrganizationId?: string;
  prosthesisTypeCode?: string;
  sentFrom?: string;
  sentTo?: string;
}

export interface CasesFilterOptions {
  clinics: Array<{ id: string; name: string }>;
  prosthesisTypes: Array<{ code: string }>;
}

export interface PaginatedLabCases {
  items: LabCaseListItem[];
  pagination: {
    page: number;
    limit: number;
    total: number;
    totalPages: number;
  };
}

export type TaskSortField =
  | 'date'
  | 'status'
  | 'clinic'
  | 'patient'
  | 'important'
  | 'prosthesis'
  | 'taskType'
  | 'dueDate';

export interface ListLabTasksParams {
  q?: string;
  clinicOrganizationId?: string;
  status?: LabTaskStatus;
  completed?: boolean;
  important?: boolean;
  pinImportant?: boolean;
  assignedToMe?: boolean;
  overdue?: boolean;
  unassignedOnly?: boolean;
  prosthesisTypeCode?: string;
  origin?: 'CLINIC_DISPATCH' | 'LAB_INTERNAL';
  sentFrom?: string;
  sentTo?: string;
  stepCompleted?: string;
  labCaseId?: string;
  sortBy?: TaskSortField;
  sortDir?: 'asc' | 'desc';
  page?: number;
  limit?: number;
}

export interface LocateTaskPageParams {
  taskId: string;
  q?: string;
  clinicOrganizationId?: string;
  status?: LabTaskStatus;
  important?: boolean;
  pinImportant?: boolean;
  assignedToMe?: boolean;
  overdue?: boolean;
  unassignedOnly?: boolean;
  prosthesisTypeCode?: string;
  origin?: 'CLINIC_DISPATCH' | 'LAB_INTERNAL';
  stepCompleted?: string;
  sortBy?: TaskSortField;
  sortDir?: 'asc' | 'desc';
  limit?: number;
}

export interface LocateTaskPageResult {
  page: number;
  found: boolean;
  labCaseId: string;
}

export interface LabTaskListItem {
  id: string;
  labCaseId: string;
  treatmentDetailId: string;
  teeth: string[];
  treatmentType: string;
  prosthesisTypeCode: string;
  prosthesisTypeLabel: string;
  workflowStepCode: string;
  stepOrder: number;
  stepLabel: string;
  status: LabTaskStatus;
  isImportant: boolean;
  caseDueDate: string | null;
  isCaseOverdue: boolean;
  assignee: LabTaskUser | null;
  assignedAt: string | null;
  lastStatusChangedAt: string | null;
  lastStatusChangedBy: LabTaskUser | null;
  createdAt: string;
  caseSentAt: string | null;
  origin?: 'CLINIC_DISPATCH' | 'LAB_INTERNAL';
  clinic: { id: string; name: string };
  patient: { id: string; firstName: string; lastName: string };
}

export interface TaskFilterOptions {
  clinics: { id: string; name: string }[];
  workflowSteps: { code: string; label: string }[];
}

export interface AssignableTaskStaff {
  id: string;
  name: string;
}

export interface PaginatedLabTasks {
  items: LabTaskListItem[];
  pagination: {
    page: number;
    limit: number;
    total: number;
    totalPages: number;
  };
}
