'use client';

import { useCallback, useEffect, useMemo, useRef, useState } from 'react';
import { useTranslations } from 'next-intl';
import { useRouter } from '@/i18n/navigation';
import { Button } from '@/components/ui/shared/Button';
import { Checkbox } from '@/components/ui/shared/Checkbox';
import { AppointmentsStrip } from '@/components/ui/treatment/AppointmentsStrip';
import { FdiToothChart } from '@/components/ui/treatment/FdiToothChart';
import { LabCasesDispatchPanel } from '@/components/ui/treatment/LabCasesDispatchPanel';
import { LabShipmentBlockedNotice } from '@/components/ui/treatment/LabShipmentBlockedNotice';
import { LabDispatchAttentionPanel } from '@/components/ui/treatment/LabDispatchAttentionPanel';
import { PastTreatmentsPanel } from '@/components/ui/treatment/PastTreatmentsPanel';
import { TreatmentDetailsEditor } from '@/components/ui/treatment/TreatmentDetailsEditor';
import { TreatmentPreviewCard } from '@/components/ui/treatment/TreatmentPreviewCard';
import { treatmentTypeLabelFromCatalog, treatmentTypeColor } from '@/components/shared/treatmentTypeDisplay';
import {
  addCalendarDays,
  compareLocalDayStart,
  isSameLocalCalendarDay,
  startOfLocalDay,
} from '@/components/appointments/appointmentTime';
import { appointmentsApi } from '@/lib/api/appointments';
import { treatmentCatalogApi } from '@/lib/api/treatment-catalog';
import { treatmentsApi } from '@/lib/api/treatments';
import { pickAutoAppointment } from '@/components/shared/treatmentSelection';
import {
  areDetailsPersistable,
  defaultTreatmentTypeForAppointment,
  isDetailReadyForLabDispatch,
  isEmptyDraftDetail,
  isLabDependentDetailMissingTeeth,
} from '@/components/treatment/treatmentDetailRules';
import type { LabDispatchAttentionItem } from '@/components/treatment/labDispatchAttention';
import { collectLabDispatchAttention } from '@/components/treatment/labDispatchAttention';
import { canEditTreatment, canViewTreatment, canAccessDashboardRoute } from '@/components/shared/permissions';
import { scrollWithinMainScrollContainer } from '@/components/shared/scrollWithinMain';
import { useMarkTabReadOnVisit } from '@/lib/hooks/useTabBadgeCounts';
import { notificationsApi } from '@/lib/api/notifications';
import { notifyTabBadgesChanged } from '@/lib/tabBadgeUtils';
import { getUserFacingError } from '@/components/shared/formatApiError';
import { useToast } from '@/lib/hooks/useToast';
import type { Organization } from '@/types/organization';
import type { AppointmentRecord } from '@/types/appointment';
import type { TreatmentCatalogEntry } from '@/types/treatment-catalog';
import type {
  FdiToothId,
  LabCaseDraft,
  LinkedOrganizationOption,
  PastLabCase,
  PastTreatment,
  PastTreatmentCase,
  TreatmentAppointment,
  TreatmentDetailDraft,
} from '@/types/treatment';

type WorkspaceMode = 'live' | 'historical';

function isTreatmentDayHistorical(treatmentAt: string, todayStart: Date): boolean {
  return compareLocalDayStart(new Date(treatmentAt), todayStart) < 0;
}

function withoutEmptyLabCaseDrafts(drafts: LabCaseDraft[]): LabCaseDraft[] {
  return drafts.filter((lc) => lc.sentAt || Boolean(lc.detailClientId));
}

function labCaseDraftsToPast(
  labCaseDrafts: LabCaseDraft[],
  details: TreatmentDetailDraft[],
): PastLabCase[] {
  return labCaseDrafts.map((lc) => {
    const linkedDetail = lc.detailClientId
      ? details.find((d) => d.clientId === lc.detailClientId)
      : undefined;

    return {
      id: lc.id ?? lc.clientId,
      clientId: lc.clientId,
      destinationOrganizationId: lc.destinationOrganizationId,
      sentAt: lc.sentAt ?? null,
      treatmentDetailId: linkedDetail?.id ?? null,
      detail: linkedDetail
        ? {
            id: linkedDetail.id ?? linkedDetail.clientId,
            clientId: linkedDetail.clientId,
            treatmentType: linkedDetail.treatmentType,
            teeth: linkedDetail.teeth,
          }
        : null,
      sends: lc.sends ?? [],
    };
  });
}

function enrichDetailsWithLabSendState(
  details: TreatmentDetailDraft[],
  labCaseDrafts: LabCaseDraft[],
): TreatmentDetailDraft[] {
  return details.map((detail) => {
    const sentLabCase = labCaseDrafts.find(
      (lc) => lc.sentAt && lc.detailClientId === detail.clientId,
    );
    if (!sentLabCase) return detail;
    return {
      ...detail,
      labCaseId: sentLabCase.id ?? detail.labCaseId,
      sentAt: sentLabCase.sentAt ?? detail.sentAt,
      sends: sentLabCase.sends ?? detail.sends,
      sendToOrganizationIds: sentLabCase.destinationOrganizationId
        ? [sentLabCase.destinationOrganizationId]
        : detail.sendToOrganizationIds,
    };
  });
}

function buildWorkspaceSnapshot(
  appointment: TreatmentAppointment,
  details: TreatmentDetailDraft[],
  labCaseDrafts: LabCaseDraft[],
  title: string,
  id?: string,
): PastTreatment {
  const detailsForPreview = enrichDetailsWithLabSendState(details, labCaseDrafts);
  return {
    ...detailsToPreviewTreatment(detailsForPreview, {
      id: id ?? `preview-${appointment.id}`,
      title,
      patientId: appointment.patientId,
      treatmentAt: appointment.startAt,
    }),
    appointmentId: appointment.id,
    labCases: labCaseDraftsToPast(labCaseDrafts, details),
  };
}

function newDetail(defaultTreatmentType?: string): TreatmentDetailDraft {
  return {
    clientId:
      typeof crypto !== 'undefined' && 'randomUUID' in crypto
        ? crypto.randomUUID()
        : `detail-${Date.now()}-${Math.random().toString(36).slice(2, 9)}`,
    treatmentType: defaultTreatmentType ?? '',
    teeth: [],
    comment: '',
    attachmentMetas: [],
    sendToOrganizationIds: [],
    sentAt: null,
  };
}

function newLabCaseDraft(): LabCaseDraft {
  return {
    clientId:
      typeof crypto !== 'undefined' && 'randomUUID' in crypto
        ? crypto.randomUUID()
        : `lab-${Date.now()}-${Math.random().toString(36).slice(2, 9)}`,
    destinationOrganizationId: null,
    detailClientId: null,
    toothProsthesis: [],
    attachmentIds: [],
    sentAt: null,
    sends: [],
  };
}

function mapAppointment(record: AppointmentRecord): TreatmentAppointment {
  return {
    id: record.id,
    patientId: record.patientId,
    patientFirstName: record.patient.firstName,
    patientLastName: record.patient.lastName,
    providerUserId: record.providerUserId,
    startAt: record.startAt,
    endAt: record.endAt,
    purpose: record.purpose,
  };
}

function mapDetailFromApi(d: PastTreatmentCase): TreatmentDetailDraft {
  return {
    clientId: d.clientId,
    id: d.id,
    treatmentType: d.treatmentType,
    teeth: d.teeth,
    comment: d.notes ?? '',
    attachmentMetas: d.attachmentMetas ?? [],
    labCaseId: d.labCaseId ?? null,
    taskProgress: d.taskProgress ?? null,
    sendToOrganizationIds: d.destinationOrganizationId ? [d.destinationOrganizationId] : [],
    sends: d.sends ?? [],
    sentAt: d.sentAt ?? null,
  };
}

function mapLabCaseDraftFromApi(lc: PastLabCase): LabCaseDraft {
  return {
    clientId: lc.clientId,
    id: lc.id,
    destinationOrganizationId: lc.destinationOrganizationId,
    detailClientId: lc.detail?.clientId ?? null,
    toothProsthesis: (lc.toothProsthesis ?? []).map((tp) => ({
      detailClientId: lc.detail?.clientId ?? tp.treatmentDetailId,
      tooth: tp.tooth,
      prosthesisTypeCode: tp.prosthesisTypeCode,
    })),
    attachmentIds: (lc.attachments ?? []).map((a) => a.id),
    sentAt: lc.sentAt ?? null,
    sends: lc.sends ?? [],
    dueDate: lc.dueDate ?? null,
    taskProgress: lc.taskProgress ?? null,
  };
}

function serializeDetails(details: TreatmentDetailDraft[]) {
  return JSON.stringify(
    details.map((d) => ({
      clientId: d.clientId,
      id: d.id,
      treatmentType: d.treatmentType,
      teeth: d.teeth,
      comment: d.comment,
      attachmentMetas: d.attachmentMetas,
    })),
  );
}

function isDetailsDirty(
  details: TreatmentDetailDraft[],
  savedSnapshot: string | null,
): boolean {
  if (savedSnapshot === null) {
    return details.some((d) => !isEmptyDraftDetail(d)) || details.length > 1;
  }
  return serializeDetails(details) !== savedSnapshot;
}

function detailsToPreviewTreatment(
  details: TreatmentDetailDraft[],
  meta: { title: string; patientId: string; treatmentAt: string; id?: string },
): PastTreatment {
  return {
    id: meta.id ?? 'current-draft',
    patientId: meta.patientId,
    title: meta.title,
    treatmentAt: meta.treatmentAt,
    details: details.map((d, idx) => ({
      id: d.id ?? d.clientId ?? `draft-${idx + 1}`,
      clientId: d.clientId,
      treatmentType: d.treatmentType,
      teeth: d.teeth,
      notes: d.comment || null,
      attachmentMetas: d.attachmentMetas,
      labCaseId: d.labCaseId ?? null,
      taskProgress: d.taskProgress ?? null,
      destinationOrganizationId: d.sendToOrganizationIds[0] ?? null,
      sends: d.sends ?? [],
      sentAt: d.sentAt ?? null,
    })),
    labCases: [],
    documents: [],
  };
}

interface TreatmentWorkspaceProps {
  userId: string;
  currentOrganization: Organization | null;
  initialAppointmentId?: string | null;
}

export function TreatmentWorkspace({
  userId,
  currentOrganization,
  initialAppointmentId = null,
}: TreatmentWorkspaceProps) {
  const t = useTranslations('treatment');
  const tErrors = useTranslations('errors');
  const router = useRouter();
  const { showError, showSuccess, messages: toastMessages } = useToast();
  const canView = canViewTreatment(currentOrganization);
  const canEdit = canEditTreatment(currentOrganization);
  useMarkTabReadOnVisit();

  const [stripHidden, setStripHidden] = useState(false);
  const todayStart = useMemo(() => startOfLocalDay(new Date()), []);

  const [selectedDay, setSelectedDay] = useState(() => startOfLocalDay(new Date()));
  const [appointments, setAppointments] = useState<TreatmentAppointment[]>([]);
  const [apptsLoading, setApptsLoading] = useState(false);
  const [selectionLocked, setSelectionLocked] = useState(false);
  const [selectedAppointmentId, setSelectedAppointmentId] = useState<string | null>(null);

  const [history, setHistory] = useState<PastTreatment[]>([]);
  const [historyLoading, setHistoryLoading] = useState(false);
  const [historyPatientId, setHistoryPatientId] = useState<string | null>(null);

  const [orgs, setOrgs] = useState<LinkedOrganizationOption[]>([]);
  const [labDependentCodes, setLabDependentCodes] = useState<Set<string>>(new Set());
  const [treatmentCatalog, setTreatmentCatalog] = useState<TreatmentCatalogEntry[]>([]);
  const treatmentDropdownCatalog = useMemo(
    () => treatmentCatalog.filter((entry) => entry.availableInTreatment),
    [treatmentCatalog],
  );

  const [details, setDetails] = useState<TreatmentDetailDraft[]>(() => [newDetail()]);
  const [labCaseDrafts, setLabCaseDrafts] = useState<LabCaseDraft[]>([]);
  const [activeDetailId, setActiveDetailId] = useState<string>(() => details[0].clientId);
  const [activeLabCaseId, setActiveLabCaseId] = useState<string | null>(null);
  const [savedSnapshot, setSavedSnapshot] = useState<string | null>(null);
  const [saveStatus, setSaveStatus] = useState<'idle' | 'dirty' | 'saving' | 'saved' | 'error'>('idle');
  const [selectedPreviewId, setSelectedPreviewId] = useState<string | null>(null);
  const [workspaceMode, setWorkspaceMode] = useState<WorkspaceMode>('live');

  const selectionLockedRef = useRef(selectionLocked);
  selectionLockedRef.current = selectionLocked;

  const detailsRef = useRef(details);
  detailsRef.current = details;
  const savedSnapshotRef = useRef(savedSnapshot);
  savedSnapshotRef.current = savedSnapshot;
  const autosaveTimerRef = useRef<ReturnType<typeof setTimeout> | null>(null);
  const saveInFlightRef = useRef(false);
  const saveQueuedRef = useRef(false);
  const draftHydratingRef = useRef(false);
  const workspaceModeRef = useRef(workspaceMode);
  workspaceModeRef.current = workspaceMode;
  const labCaseDraftsRef = useRef(labCaseDrafts);
  labCaseDraftsRef.current = labCaseDrafts;
  const skipNextGetDraftRef = useRef(false);
  const pendingAppointmentIdRef = useRef<string | null>(initialAppointmentId);
  const labPanelRef = useRef<HTMLDivElement>(null);
  const historyRequestRef = useRef(0);

  useEffect(() => {
    pendingAppointmentIdRef.current = initialAppointmentId;
    if (initialAppointmentId) {
      setSelectedDay(startOfLocalDay(new Date()));
      setSelectionLocked(false);
    }
  }, [initialAppointmentId]);

  const [sendBusyId, setSendBusyId] = useState<string | null>(null);
  const [uploadBusyDetailId, setUploadBusyDetailId] = useState<string | null>(null);
  const [organizationSearch, setOrganizationSearch] = useState('');
  const [recentOrganizationIds, setRecentOrganizationIds] = useState<string[]>([]);
  const [showWholeTreatmentPlan, setShowWholeTreatmentPlan] = useState(false);

  const isDetailLocked = useCallback(
    (detail: TreatmentDetailDraft) =>
      labCaseDrafts.some((lc) => lc.sentAt && lc.detailClientId === detail.clientId),
    [labCaseDrafts],
  );

  const activeSentLabCaseId = useMemo(() => {
    const match = labCaseDrafts.find(
      (lc) => lc.detailClientId === activeDetailId && lc.sentAt && lc.id,
    );
    return match?.id ?? null;
  }, [labCaseDrafts, activeDetailId]);

  useEffect(() => {
    if (!activeSentLabCaseId) return;
    void notificationsApi.markCaseRead(activeSentLabCaseId).then(() => notifyTabBadgesChanged());
  }, [activeSentLabCaseId]);

  const isDirty = useMemo(
    () => isDetailsDirty(details, savedSnapshot),
    [details, savedSnapshot],
  );

  const AUTOSAVE_DEBOUNCE_MS = 600;

  const selectedAppointment = useMemo(
    () => appointments.find((a) => a.id === selectedAppointmentId) ?? null,
    [appointments, selectedAppointmentId],
  );

  const isViewingPastDay = useMemo(
    () => compareLocalDayStart(selectedDay, todayStart) < 0,
    [selectedDay, todayStart],
  );

  const canEditTreatmentForDay =
    canEdit &&
    Boolean(selectedAppointment) &&
    !isViewingPastDay &&
    workspaceMode === 'live';

  const historyPanelItems = history;

  const activeDetail = useMemo(
    () => details.find((d) => d.clientId === activeDetailId) ?? details[0] ?? null,
    [details, activeDetailId],
  );

  const showLabDispatchPanel = useMemo(
    () => details.some((d) => isDetailReadyForLabDispatch(d, labDependentCodes)),
    [details, labDependentCodes],
  );

  const showLabShipmentBlocked = useMemo(
    () =>
      Boolean(
        activeDetail && isLabDependentDetailMissingTeeth(activeDetail, labDependentCodes),
      ),
    [activeDetail, labDependentCodes],
  );

  const currentDraftPreview = useMemo<PastTreatment | null>(() => {
    if (!selectedAppointment) return null;
    return buildWorkspaceSnapshot(
      selectedAppointment,
      details,
      labCaseDrafts,
      t('treatmentPlanTitle', {
        patientName: `${selectedAppointment.patientFirstName} ${selectedAppointment.patientLastName}`,
      }),
      'current-draft',
    );
  }, [details, labCaseDrafts, selectedAppointment, t]);

  const previewTreatment = useMemo(() => {
    if (!selectedPreviewId) return currentDraftPreview;
    return (
      history.find((item) => item.id === selectedPreviewId) ??
      historyPanelItems.find((item) => item.id === selectedPreviewId) ??
      currentDraftPreview
    );
  }, [selectedPreviewId, history, historyPanelItems, currentDraftPreview]);

  const isBrowsing = selectedPreviewId !== null;

  const previewHeading = isBrowsing
    ? t('previewBrowsingTitle')
    : t('previewCurrentDraft');

  const labAttentionItems = useMemo(
    () =>
      collectLabDispatchAttention(
        labDependentCodes,
        currentDraftPreview,
        history,
        selectedAppointmentId,
      ),
    [labDependentCodes, currentDraftPreview, history, selectedAppointmentId],
  );

  const hydrateFromTreatment = useCallback((treatment: PastTreatment) => {
    const mapped = treatment.details.map(mapDetailFromApi);
    setDetails(mapped);
    setActiveDetailId((prev) => {
      const stillExists = mapped.some((d) => d.clientId === prev);
      return stillExists ? prev : mapped[0]?.clientId ?? prev;
    });
    setSavedSnapshot(serializeDetails(mapped));
    const mappedLabCases = withoutEmptyLabCaseDrafts(
      (treatment.labCases ?? []).map(mapLabCaseDraftFromApi),
    );
    setLabCaseDrafts(mappedLabCases);
    setActiveLabCaseId(mappedLabCases[0]?.clientId ?? null);
    setOrganizationSearch('');
    setSaveStatus('idle');
  }, []);

  const selectedTeethSet = useMemo(() => new Set(activeDetail?.teeth ?? []), [activeDetail?.teeth]);

  const wholePlanTeethSet = useMemo(() => {
    const set = new Set<FdiToothId>();
    for (const detail of details) {
      for (const tooth of detail.teeth) set.add(tooth);
    }
    return set;
  }, [details]);

  const wholePlanToothColors = useMemo(() => {
    const colors: Partial<Record<FdiToothId, string>> = {};
    for (let i = 0; i < details.length; i++) {
      const detail = details[i];
      const catalogIndex = treatmentCatalog.findIndex((e) => e.code === detail.treatmentType);
      const color = treatmentTypeColor(detail.treatmentType, catalogIndex >= 0 ? catalogIndex : i);
      for (const tooth of detail.teeth) {
        if (!(tooth in colors)) colors[tooth] = color;
      }
    }
    return colors;
  }, [details, treatmentCatalog]);

  const chartSelectedTeeth = showWholeTreatmentPlan ? wholePlanTeethSet : selectedTeethSet;
  const chartToothColors = showWholeTreatmentPlan ? wholePlanToothColors : undefined;

  // Reset whole-plan overview when switching details.
  useEffect(() => {
    setShowWholeTreatmentPlan(false);
  }, [activeDetailId]);

  // Sync active lab shipment when the selected treatment detail changes.
  useEffect(() => {
    const match = labCaseDrafts.find((lc) => lc.detailClientId === activeDetailId);
    setActiveLabCaseId(match?.clientId ?? null);
  }, [activeDetailId, labCaseDrafts]);

  useEffect(() => {
    let cancelled = false;
    setApptsLoading(true);
    void (async () => {
      try {
        const dayStart = startOfLocalDay(selectedDay);
        const dayEnd = addCalendarDays(dayStart, 1);
        const response = await appointmentsApi.list({
          from: dayStart.toISOString(),
          to: dayEnd.toISOString(),
        });
        if (cancelled) return;
        const list = response.data
          .filter((a) => a.providerUserId === userId)
          .map(mapAppointment);
        setAppointments(list);
        if (!selectionLockedRef.current) {
          const pendingId = pendingAppointmentIdRef.current;
          if (pendingId && list.some((appointment) => appointment.id === pendingId)) {
            setSelectedAppointmentId(pendingId);
            setSelectionLocked(true);
            pendingAppointmentIdRef.current = null;
            router.replace('/treatment', { scroll: false });
          } else {
            pendingAppointmentIdRef.current = null;
            setSelectedAppointmentId(pickAutoAppointment(list, selectedDay));
          }
        }
      } catch (error: unknown) {
        if (!cancelled) {
          showError(getUserFacingError(error, tErrors, t('errorLoadAppointments')));
        }
      } finally {
        if (!cancelled) setApptsLoading(false);
      }
    })();
    return () => {
      cancelled = true;
    };
  }, [userId, selectedDay, showError, t, router]);

  useEffect(() => {
    const today = startOfLocalDay(new Date());
    if (!isSameLocalCalendarDay(selectedDay, today) || selectionLocked) return;

    const id = window.setInterval(() => {
      setSelectedAppointmentId((prev) => {
        const next = pickAutoAppointment(appointments, selectedDay);
        return next ?? prev;
      });
    }, 60_000);

    return () => window.clearInterval(id);
  }, [selectedDay, appointments, selectionLocked]);

  useEffect(() => {
    let cancelled = false;
    void (async () => {
      try {
        const [orgsResponse, catalogResponse] = await Promise.all([
          treatmentsApi.listLinkedOrganizations(),
          treatmentCatalogApi.list(),
        ]);
        if (cancelled) return;
        setOrgs(orgsResponse.data);
        setTreatmentCatalog(catalogResponse.data);
        setLabDependentCodes(
          new Set(catalogResponse.data.filter((entry) => entry.labDependent).map((entry) => entry.code)),
        );
      } catch (error: unknown) {
        if (!cancelled) {
          showError(getUserFacingError(error, tErrors, t('errorLoadOrgs')));
        }
      }
    })();
    return () => {
      cancelled = true;
    };
  }, [showError, t]);

  useEffect(() => {
    if (!selectedAppointment?.patientId) {
      setHistoryPatientId(null);
      setHistory([]);
      setHistoryLoading(false);
      return;
    }
    const nextPatientId = selectedAppointment.patientId;
    setHistoryPatientId((prev) => {
      if (prev !== nextPatientId) {
        setHistory([]);
        setHistoryLoading(true);
      }
      return nextPatientId;
    });
  }, [selectedAppointment?.patientId]);

  useEffect(() => {
    if (!historyPatientId) return;
    const requestId = ++historyRequestRef.current;
    setHistoryLoading(true);
    void (async () => {
      try {
        const response = await treatmentsApi.listPatientHistory(historyPatientId, 50);
        if (requestId !== historyRequestRef.current) return;
        setHistory(response.data);
      } catch (error: unknown) {
        if (requestId !== historyRequestRef.current) return;
        showError(getUserFacingError(error, tErrors, t('errorLoadHistory')));
      } finally {
        if (requestId === historyRequestRef.current) {
          setHistoryLoading(false);
        }
      }
    })();
  }, [historyPatientId, showError, t]);

  useEffect(() => {
    const appointmentId = selectedAppointment?.id;
    if (!appointmentId || workspaceMode !== 'live') return;

    if (skipNextGetDraftRef.current) {
      skipNextGetDraftRef.current = false;
      return;
    }

    let cancelled = false;
    draftHydratingRef.current = true;
    if (autosaveTimerRef.current) {
      clearTimeout(autosaveTimerRef.current);
      autosaveTimerRef.current = null;
    }

    void (async () => {
      try {
        const response = await treatmentsApi.getDraft(appointmentId);
        if (cancelled) return;

        if (response.data?.details?.length) {
          const mapped = response.data.details.map(mapDetailFromApi);
          setDetails(mapped);
          setActiveDetailId((prev) => {
            const stillExists = mapped.some((d) => d.clientId === prev);
            return stillExists ? prev : mapped[0].clientId;
          });
          setSavedSnapshot(serializeDetails(mapped));
        } else {
          const first = newDetail(
            defaultTreatmentTypeForAppointment(selectedAppointment?.purpose, treatmentCatalog),
          );
          setDetails([first]);
          setActiveDetailId(first.clientId);
          setSavedSnapshot(serializeDetails([first]));
        }

        const mappedLabCases = withoutEmptyLabCaseDrafts(
          (response.data?.labCases ?? []).map(mapLabCaseDraftFromApi),
        );
        setLabCaseDrafts(mappedLabCases);
        setActiveLabCaseId(mappedLabCases[0]?.clientId ?? null);
        setOrganizationSearch('');
        setSaveStatus('idle');
      } catch (error: unknown) {
        if (!cancelled) {
          showError(getUserFacingError(error, tErrors, t('errorLoadDraft')));
        }
      } finally {
        if (!cancelled) {
          draftHydratingRef.current = false;
        }
      }
    })();
    return () => {
      cancelled = true;
      draftHydratingRef.current = false;
    };
  }, [selectedAppointment?.id, selectedAppointment?.purpose, workspaceMode, treatmentCatalog, showError, t]);

  const persistDraft = useCallback(
    async (options?: { force?: boolean }) => {
      if (!selectedAppointment) throw new Error('No appointment selected');

      const currentDetails = detailsRef.current;
      const dirty = isDetailsDirty(currentDetails, savedSnapshotRef.current);

      if (!options?.force && !dirty) {
        return detailsToPreviewTreatment(currentDetails, {
          title: t('treatmentPlanTitle', {
            patientName: `${selectedAppointment.patientFirstName} ${selectedAppointment.patientLastName}`,
          }),
          patientId: selectedAppointment.patientId,
          treatmentAt: selectedAppointment.startAt,
        });
      }

      if (!areDetailsPersistable(currentDetails)) {
        return detailsToPreviewTreatment(currentDetails, {
          title: t('treatmentPlanTitle', {
            patientName: `${selectedAppointment.patientFirstName} ${selectedAppointment.patientLastName}`,
          }),
          patientId: selectedAppointment.patientId,
          treatmentAt: selectedAppointment.startAt,
        });
      }

      const response = await treatmentsApi.saveDraft(selectedAppointment.id, {
        details: currentDetails.map(({ clientId, id, treatmentType, teeth, comment, attachmentMetas }) => ({
          clientId,
          id,
          treatmentType,
          teeth,
          comment,
          attachmentIds: attachmentMetas.map((a) => a.id),
        })),
      });
      const mapped = response.data.details.map(mapDetailFromApi);
      setDetails(mapped);
      setActiveDetailId((prev) => {
        const stillExists = mapped.some((d) => d.clientId === prev);
        return stillExists ? prev : mapped[0]?.clientId ?? prev;
      });
      setSavedSnapshot(serializeDetails(mapped));
      return response.data;
    },
    [selectedAppointment, t],
  );

  const refreshHistory = useCallback(async (patientId: string) => {
    const requestId = ++historyRequestRef.current;
    try {
      const response = await treatmentsApi.listPatientHistory(patientId, 50);
      if (requestId !== historyRequestRef.current) return;
      setHistory(response.data);
    } catch (error: unknown) {
      if (requestId !== historyRequestRef.current) return;
      showError(getUserFacingError(error, tErrors, t('errorLoadHistory')));
    }
  }, [showError, t]);

  const runDraftSave = useCallback(async () => {
    if (!selectedAppointment || saveInFlightRef.current) {
      if (saveInFlightRef.current) saveQueuedRef.current = true;
      return;
    }

    if (
      !isDetailsDirty(detailsRef.current, savedSnapshotRef.current) ||
      !areDetailsPersistable(detailsRef.current)
    ) {
      return;
    }

    saveInFlightRef.current = true;
    setSaveStatus('saving');
    try {
      await persistDraft();
      setSaveStatus('saved');
      if (historyPatientId) {
        await refreshHistory(historyPatientId);
      }
    } catch (error: unknown) {
      setSaveStatus('error');
      showError(getUserFacingError(error, tErrors, t('errorSaveDraft')));
      throw error;
    } finally {
      saveInFlightRef.current = false;
      if (saveQueuedRef.current) {
        saveQueuedRef.current = false;
        if (isDetailsDirty(detailsRef.current, savedSnapshotRef.current)) {
          void runDraftSave();
        }
      }
    }
  }, [selectedAppointment, persistDraft, showError, t, historyPatientId, refreshHistory]);

  const flushDraftSave = useCallback(async (): Promise<boolean> => {
    if (autosaveTimerRef.current) {
      clearTimeout(autosaveTimerRef.current);
      autosaveTimerRef.current = null;
    }

    if (workspaceModeRef.current !== 'live' || !selectedAppointment || !canEditTreatmentForDay) {
      return true;
    }

    while (saveInFlightRef.current) {
      await new Promise((resolve) => setTimeout(resolve, 50));
    }

    if (!isDetailsDirty(detailsRef.current, savedSnapshotRef.current)) {
      return true;
    }

    try {
      await runDraftSave();
      return true;
    } catch {
      return window.confirm(t('confirmDiscard'));
    }
  }, [selectedAppointment, canEditTreatmentForDay, runDraftSave, t]);

  useEffect(() => {
    if (draftHydratingRef.current || !canEditTreatmentForDay || !selectedAppointment?.id) {
      return;
    }

    if (!isDirty) {
      return;
    }

    setSaveStatus('dirty');

    if (autosaveTimerRef.current) clearTimeout(autosaveTimerRef.current);
    autosaveTimerRef.current = setTimeout(() => {
      autosaveTimerRef.current = null;
      void runDraftSave();
    }, AUTOSAVE_DEBOUNCE_MS);

    return () => {
      if (autosaveTimerRef.current) {
        clearTimeout(autosaveTimerRef.current);
        autosaveTimerRef.current = null;
      }
    };
  }, [details, isDirty, canEditTreatmentForDay, selectedAppointment?.id, runDraftSave]);

  const resetToLiveContext = useCallback(() => {
    setWorkspaceMode('live');
    setSelectedPreviewId(null);
  }, []);

  const onPickAppointment = useCallback(
    (id: string) => {
      void (async () => {
        const ok = await flushDraftSave();
        if (!ok) return;
        resetToLiveContext();
        setSelectionLocked(true);
        setSelectedAppointmentId(id);
      })();
    },
    [flushDraftSave, resetToLiveContext],
  );

  const canAccessOrganizations = canAccessDashboardRoute(currentOrganization, '/organizations');

  const onSelectDay = useCallback(
    (day: Date) => {
      void (async () => {
        const ok = await flushDraftSave();
        if (!ok) return;
        const patientIdToRefresh = historyPatientId;
        resetToLiveContext();
        setSelectionLocked(false);
        setSelectedDay(startOfLocalDay(day));
        if (patientIdToRefresh) {
          await refreshHistory(patientIdToRefresh);
        }
      })();
    },
    [flushDraftSave, resetToLiveContext, historyPatientId, refreshHistory],
  );

  const handleSelectPreviewTreatment = useCallback((treatment: PastTreatment) => {
    setSelectedPreviewId(treatment.id);
  }, []);

  const exitBrowse = useCallback(() => {
    setSelectedPreviewId(null);
  }, []);

  const loadTreatmentIntoWorkspace = useCallback(
    async (treatment: PastTreatment, focusDetailClientId?: string) => {
      if (!treatment.appointmentId) {
        showError(t('errorNoAppointmentForTreatment'));
        return false;
      }

      const ok = workspaceModeRef.current === 'live' ? await flushDraftSave() : true;
      if (!ok) return false;

      const isHistorical = isTreatmentDayHistorical(treatment.treatmentAt, todayStart);
      setWorkspaceMode(isHistorical ? 'historical' : 'live');
      setSelectedPreviewId(null);
      setSelectedDay(startOfLocalDay(new Date(treatment.treatmentAt)));
      setSelectionLocked(true);
      setSelectedAppointmentId(treatment.appointmentId);

      skipNextGetDraftRef.current = true;
      draftHydratingRef.current = true;
      hydrateFromTreatment(treatment);
      draftHydratingRef.current = false;

      if (focusDetailClientId) {
        setActiveDetailId(focusDetailClientId);
        const mappedLabCases = withoutEmptyLabCaseDrafts(
          (treatment.labCases ?? []).map(mapLabCaseDraftFromApi),
        );
        const linked = mappedLabCases.find(
          (lc) => !lc.sentAt && lc.detailClientId === focusDetailClientId,
        );
        if (linked) {
          setActiveLabCaseId(linked.clientId);
        }
        requestAnimationFrame(() => {
          scrollWithinMainScrollContainer(labPanelRef.current);
        });
      }

      return true;
    },
    [flushDraftSave, hydrateFromTreatment, showError, t, todayStart],
  );

  const handleLoadIntoWorkspace = useCallback(() => {
    if (!previewTreatment) return;
    void loadTreatmentIntoWorkspace(previewTreatment);
  }, [loadTreatmentIntoWorkspace, previewTreatment]);

  const handleGoToLabDispatch = useCallback(
    (item: LabDispatchAttentionItem) => {
      if (item.isCurrentDraft) {
        exitBrowse();
        setActiveDetailId(item.detailClientId);
        const linked = labCaseDrafts.find(
          (lc) => !lc.sentAt && lc.detailClientId === item.detailClientId,
        );
        if (linked) {
          setActiveLabCaseId(linked.clientId);
        }
        requestAnimationFrame(() => {
          scrollWithinMainScrollContainer(labPanelRef.current);
        });
        return;
      }

      const treatment =
        history.find((entry) => entry.id === item.treatmentId) ??
        historyPanelItems.find((entry) => entry.id === item.treatmentId);
      if (!treatment) return;
      void loadTreatmentIntoWorkspace(treatment, item.detailClientId);
    },
    [exitBrowse, history, historyPanelItems, labCaseDrafts, loadTreatmentIntoWorkspace],
  );

  const uploadForDetail = useCallback(
    async (detailClientId: string, files: FileList | File[]) => {
      if (!canEditTreatmentForDay || !selectedAppointment) return;
      const list = files instanceof FileList ? Array.from(files) : files;
      if (!list.length) return;

      setUploadBusyDetailId(detailClientId);
      try {
        const uploaded = await treatmentsApi.uploadCaseAttachments(
          selectedAppointment.id,
          detailClientId,
          list,
        );
        setDetails((prev) =>
          prev.map((d) =>
            d.clientId === detailClientId
              ? { ...d, attachmentMetas: [...d.attachmentMetas, ...uploaded.data] }
              : d,
          ),
        );
        showSuccess(t('successFilesUploaded', { count: uploaded.data.length }));
      } catch (error: unknown) {
        showError(getUserFacingError(error, tErrors, t('errorUpload')));
      } finally {
        setUploadBusyDetailId(null);
      }
    },
    [canEditTreatmentForDay, selectedAppointment, showSuccess, showError, t],
  );

  const persistLabCases = useCallback(
    async (savedTreatment: PastTreatment, draftsOverride?: LabCaseDraft[]) => {
      if (!selectedAppointment) throw new Error('No appointment selected');

      const drafts = draftsOverride ?? labCaseDrafts;
      const detailIdByClientId = new Map(
        savedTreatment.details.map((d) => [d.clientId, d.id]),
      );

      const payload = drafts
        .map((lc) => {
          if (!lc.detailClientId) return null;
          const treatmentDetailId = detailIdByClientId.get(lc.detailClientId);
          if (!treatmentDetailId) return null;

          return {
            clientId: lc.clientId,
            id: lc.id,
            destinationOrganizationId: lc.destinationOrganizationId ?? undefined,
            treatmentDetailId,
            toothProsthesis: lc.toothProsthesis
              .map((tp) => {
                const detailId = detailIdByClientId.get(tp.detailClientId);
                if (!detailId) return null;
                return {
                  treatmentDetailId: detailId,
                  tooth: tp.tooth,
                  prosthesisTypeCode: tp.prosthesisTypeCode,
                };
              })
              .filter(
                (row): row is { treatmentDetailId: string; tooth: string; prosthesisTypeCode: string } =>
                  row !== null,
              ),
            attachmentIds: lc.attachmentIds,
            dueDate: lc.dueDate ?? null,
          };
        })
        .filter((row): row is NonNullable<typeof row> => row !== null);

      if (payload.length === 0) {
        return savedTreatment;
      }

      const response = await treatmentsApi.saveLabCases(selectedAppointment.id, {
        labCases: payload,
      });
      const mapped = withoutEmptyLabCaseDrafts(response.data.labCases.map(mapLabCaseDraftFromApi));
      setLabCaseDrafts(mapped);
      setActiveLabCaseId((prev) => {
        if (prev && mapped.some((lc) => lc.clientId === prev)) return prev;
        return mapped[0]?.clientId ?? null;
      });
      return response.data;
    },
    [labCaseDrafts, selectedAppointment],
  );

  const handleLabCasesChange = useCallback(
    (next: LabCaseDraft[]) => {
      const prevCleaned = withoutEmptyLabCaseDrafts(labCaseDrafts);
      const cleaned = withoutEmptyLabCaseDrafts(next);
      setLabCaseDrafts(cleaned);

      if (!cleaned.some((lc) => lc.detailClientId === activeDetailId)) {
        setActiveLabCaseId((prev) =>
          prev && cleaned.some((lc) => lc.clientId === prev) ? prev : null,
        );
      }

      const removedPersistedDraft = prevCleaned.some(
        (lc) => lc.id && !cleaned.some((row) => row.clientId === lc.clientId),
      );
      if (
        removedPersistedDraft &&
        selectedAppointment &&
        canEditTreatmentForDay
      ) {
        void (async () => {
          try {
            const saved = await persistDraft({ force: true });
            await persistLabCases(saved, cleaned);
          } catch (error: unknown) {
            showError(getUserFacingError(error, tErrors, t('errorSaveLabShipments')));
          }
        })();
      }
    },
    [
      activeDetailId,
      canEditTreatmentForDay,
      labCaseDrafts,
      persistDraft,
      persistLabCases,
      selectedAppointment,
      showError,
      t,
    ],
  );

  const handleAddLabCase = useCallback(async () => {
    if (!canEditTreatmentForDay || !selectedAppointment) return;

    const cleaned = withoutEmptyLabCaseDrafts(labCaseDrafts);
    const existing = cleaned.find(
      (lc) => !lc.sentAt && lc.detailClientId === activeDetailId,
    );
    if (existing) {
      setActiveLabCaseId(existing.clientId);
      return;
    }

    const activeDetail = details.find((d) => d.clientId === activeDetailId);
    if (
      activeDetail &&
      isLabDependentDetailMissingTeeth(activeDetail, labDependentCodes)
    ) {
      showError(t('labShipmentBlockedBody'));
      return;
    }

    const shouldIncludeActive = Boolean(
      activeDetail && isDetailReadyForLabDispatch(activeDetail, labDependentCodes),
    );

    const orphan = cleaned.find((lc) => !lc.sentAt && !lc.detailClientId);
    if (orphan && shouldIncludeActive) {
      const updatedLabCases = cleaned.map((lc) =>
        lc.clientId === orphan.clientId ? { ...lc, detailClientId: activeDetailId } : lc,
      );
      setLabCaseDrafts(updatedLabCases);
      setActiveLabCaseId(orphan.clientId);

      try {
        const saved = await persistDraft({ force: true });
        await persistLabCases(saved, updatedLabCases);
      } catch (error: unknown) {
        showError(getUserFacingError(error, tErrors, t('errorSaveLabShipments')));
      }
      return;
    }

    const next: LabCaseDraft = {
      ...newLabCaseDraft(),
      detailClientId: shouldIncludeActive ? activeDetailId : null,
    };
    const updatedLabCases = [...cleaned, next];
    setLabCaseDrafts(updatedLabCases);
    setActiveLabCaseId(next.clientId);

    try {
      const saved = await persistDraft({ force: true });
      await persistLabCases(saved, updatedLabCases);
    } catch (error: unknown) {
      showError(getUserFacingError(error, tErrors, t('errorSaveLabShipments')));
    }
  }, [
    activeDetailId,
    canEditTreatmentForDay,
    details,
    labCaseDrafts,
    labDependentCodes,
    persistDraft,
    persistLabCases,
    selectedAppointment,
    showError,
    t,
  ]);

  const handleSendLabCase = useCallback(
    async (labCase: LabCaseDraft, comment?: string) => {
      if (!canEditTreatmentForDay || !selectedAppointment) return;
      if (!labCase.destinationOrganizationId) {
        showError(t('errorChooseOrg'));
        return;
      }
      if (!labCase.detailClientId) {
        showError(t('errorLabCaseNeedsDetails'));
        return;
      }

      setSendBusyId(labCase.clientId);
      try {
        if (autosaveTimerRef.current) {
          clearTimeout(autosaveTimerRef.current);
          autosaveTimerRef.current = null;
        }
        while (saveInFlightRef.current) {
          await new Promise((resolve) => setTimeout(resolve, 50));
        }

        const saved = await persistDraft({ force: true });
        const afterLabCases = await persistLabCases(saved);

        const refreshedLabCase = afterLabCases.labCases.find(
          (lc) => lc.clientId === labCase.clientId || lc.id === labCase.id,
        );
        if (!refreshedLabCase?.id) throw new Error(t('errorSendCase'));

        const trimmedComment = comment?.trim();
        if (trimmedComment) {
          await treatmentsApi.addLabCaseComment(refreshedLabCase.id, { body: trimmedComment });
        }

        const response = await treatmentsApi.sendLabCase(refreshedLabCase.id);

        const draftResponse = await treatmentsApi.getDraft(selectedAppointment.id);
        if (draftResponse.data?.details?.length) {
          const mapped = draftResponse.data.details.map(mapDetailFromApi);
          setDetails(mapped);
          setSavedSnapshot(serializeDetails(mapped));
        } else {
          const sentDetailClientId = labCase.detailClientId;
          setDetails((prev) =>
            prev.map((detail) => {
              if (detail.clientId !== sentDetailClientId) return detail;
              return {
                ...detail,
                labCaseId: response.data.id,
                sentAt: response.data.sentAt,
                sends: response.data.sends,
                sendToOrganizationIds: response.data.destinationOrganizationId
                  ? [response.data.destinationOrganizationId]
                  : detail.sendToOrganizationIds,
              };
            }),
          );
        }

        setLabCaseDrafts((prev) =>
          prev.map((lc) =>
            lc.clientId === labCase.clientId
              ? {
                  ...lc,
                  id: response.data.id,
                  sentAt: response.data.sentAt,
                  destinationOrganizationId: response.data.destinationOrganizationId,
                  sends: response.data.sends,
                }
              : lc,
          ),
        );

        setRecentOrganizationIds((prev) => {
          const orgId = labCase.destinationOrganizationId!;
          return [orgId, ...prev.filter((id) => id !== orgId)].slice(0, 10);
        });
        showSuccess(t('successCaseSent'));
        notifyTabBadgesChanged();
      } catch (error: unknown) {
        showError(getUserFacingError(error, tErrors, t('errorSendCase')));
      } finally {
        setSendBusyId(null);
      }
    },
    [
      canEditTreatmentForDay,
      selectedAppointment,
      persistDraft,
      persistLabCases,
      showSuccess,
      showError,
      t,
    ],
  );

  if (!canView) {
    return (
      <div className="surface-card p-6 max-w-xl">
        <h2 className="text-lg font-semibold text-text-primary">{t('noPermissionTitle')}</h2>
        <p className="text-sm text-text-secondary mt-2">{t('noPermissionBody')}</p>
      </div>
    );
  }

  return (
    <div className="space-y-4">
      <header className="space-y-1">
        <h1 className="text-xl sm:text-2xl font-semibold text-text-primary">{t('title')}</h1>
        <p className="text-sm text-text-secondary">
          {canEdit ? t('subtitleEditPhase4') : t('subtitleReadOnly')}
        </p>
      </header>

      <AppointmentsStrip
        stripHidden={stripHidden}
        onToggleStripHidden={() => setStripHidden((s) => !s)}
        selectedDay={selectedDay}
        onSelectDay={onSelectDay}
        appointments={appointments}
        selectedAppointmentId={selectedAppointmentId}
        onSelectAppointment={onPickAppointment}
        treatmentCatalog={treatmentCatalog}
        loading={apptsLoading}
      />

      {workspaceMode === 'historical' && (
        <p className="text-sm text-emerald-700 dark:text-emerald-400 rounded-[var(--radius-md)] border border-emerald-500/40 bg-emerald-500/10 px-3 py-2">
          {t('historicalReadonlyNotice')}
        </p>
      )}

      {isViewingPastDay && workspaceMode === 'live' && (
        <p className="text-sm text-text-secondary rounded-[var(--radius-md)] border border-border/60 bg-background-secondary/50 px-3 py-2">
          {t('pastDayNotice')}
        </p>
      )}

      <div className="grid grid-cols-1 xl:grid-cols-[minmax(300px,380px)_minmax(0,1fr)] gap-4 items-start">
        <div className="space-y-3 min-w-0 xl:max-w-[380px]">
          {selectedAppointment ? (
            <div className="surface-card p-3 space-y-0.5">
              <p className="text-[10px] uppercase tracking-wide text-text-muted">{t('selectedPatient')}</p>
              <p className="text-base font-semibold text-text-primary">
                {selectedAppointment.patientFirstName} {selectedAppointment.patientLastName}
              </p>
              <p className="text-[11px] text-text-secondary">
                {t('purposeLabel')}{' '}
                <span className="text-text-primary">
                  {treatmentTypeLabelFromCatalog(selectedAppointment.purpose, treatmentCatalog)}
                </span>
              </p>
            </div>
          ) : (
            <div className="surface-card p-3 text-sm text-text-muted">
              {apptsLoading ? t('loadingAppointments') : t('selectDayWithAppointment')}
            </div>
          )}

          <LabDispatchAttentionPanel
            items={labAttentionItems}
            treatmentCatalog={treatmentCatalog}
            labDependentCodes={labDependentCodes}
            orgs={orgs}
            onGoToDispatch={handleGoToLabDispatch}
          />

          {isBrowsing && previewTreatment ? (
            <div className="rounded-[var(--radius-md)] border border-primary/40 bg-primary/5 px-3 py-3 space-y-3">
              <p className="text-sm text-text-primary">
                {t('browseBanner', {
                  date: new Date(previewTreatment.treatmentAt).toLocaleDateString(undefined, {
                    weekday: 'short',
                    year: 'numeric',
                    month: 'short',
                    day: 'numeric',
                  }),
                })}
              </p>
              <div className="flex flex-col gap-2 sm:flex-row sm:flex-wrap">
                <Button type="button" variant="primary" onClick={handleLoadIntoWorkspace}>
                  {t('loadIntoWorkspace')}
                </Button>
                <Button type="button" variant="ghost" onClick={exitBrowse}>
                  {t('backToCurrentDraft')}
                </Button>
              </div>
            </div>
          ) : null}

          <TreatmentPreviewCard
            treatment={previewTreatment}
            heading={previewHeading}
            labDependentCodes={labDependentCodes}
            treatmentCatalog={treatmentCatalog}
            orgs={orgs}
          />

          <PastTreatmentsPanel
            items={historyPanelItems}
            currentDraft={
              workspaceMode === 'live' && !isBrowsing ? currentDraftPreview : null
            }
            patientName={
              selectedAppointment
                ? `${selectedAppointment.patientFirstName} ${selectedAppointment.patientLastName}`
                : undefined
            }
            currentAppointmentId={selectedAppointmentId}
            treatmentCatalog={treatmentCatalog}
            labDependentCodes={labDependentCodes}
            orgs={orgs}
            loading={historyLoading}
            selectedPreviewId={selectedPreviewId}
            onSelectTreatment={handleSelectPreviewTreatment}
          />
        </div>

        <div className="space-y-3 min-w-0 w-full">
          <FdiToothChart
            selected={chartSelectedTeeth}
            toothColors={chartToothColors}
            readOnly={showWholeTreatmentPlan}
            headerControl={
              details.length > 1 ? (
                <Checkbox
                  checked={showWholeTreatmentPlan}
                  onChange={setShowWholeTreatmentPlan}
                  label={t('toothChartWholePlan')}
                  className="text-[11px] [&_span:last-child]:text-[11px] [&_span:last-child]:text-text-muted"
                />
              ) : undefined
            }
            onToggle={(fdi) => {
              if (!canEditTreatmentForDay || isDetailLocked(activeDetail) || showWholeTreatmentPlan) return;
              setDetails((prev) =>
                prev.map((d) => {
                  if (d.clientId !== activeDetailId) return d;
                  const set = new Set(d.teeth);
                  if (set.has(fdi)) set.delete(fdi);
                  else set.add(fdi);
                  return { ...d, teeth: [...set].sort() as FdiToothId[] };
                }),
              );
            }}
            disabled={!canEditTreatmentForDay || isDetailLocked(activeDetail)}
          />

          <TreatmentDetailsEditor
            details={details}
            activeDetailId={activeDetailId}
            onActiveDetailChange={setActiveDetailId}
            onDetailsChange={setDetails}
            isDetailLocked={isDetailLocked}
            labDependentCodes={labDependentCodes}
            treatmentCatalog={treatmentDropdownCatalog}
            disabled={!canEditTreatmentForDay}
            canEdit={canEdit}
            saveStatus={saveStatus}
            uploadBusy={uploadBusyDetailId === activeDetailId}
            onAddDetail={() => {
              const next = newDetail(
                defaultTreatmentTypeForAppointment(selectedAppointment?.purpose, treatmentCatalog),
              );
              setDetails((prev) => [...prev, next]);
              setActiveDetailId(next.clientId);
            }}
            onUploadFiles={(files) => void uploadForDetail(activeDetailId, files ?? [])}
            onCommentError={showError}
          />

          <div ref={labPanelRef}>
          {showLabShipmentBlocked ? <LabShipmentBlockedNotice /> : null}
          {showLabDispatchPanel ? (
          <LabCasesDispatchPanel
            details={details}
            activeDetailId={activeDetailId}
            labCases={labCaseDrafts}
            labDependentCodes={labDependentCodes}
            treatmentCatalog={treatmentCatalog}
            activeLabCaseId={activeLabCaseId}
            onLabCasesChange={handleLabCasesChange}
            disabled={!canEditTreatmentForDay}
            canEdit={canEdit}
            orgs={orgs}
            organizationSearch={organizationSearch}
            onOrganizationSearchChange={setOrganizationSearch}
            recentOrganizationIds={recentOrganizationIds}
            onRecentOrganizationPick={(orgId) => {
              setLabCaseDrafts((prev) => {
                const targetId =
                  activeLabCaseId ??
                  prev.find((lc) => !lc.sentAt && lc.detailClientId === activeDetailId)
                    ?.clientId;
                if (!targetId) return prev;
                return prev.map((lc) =>
                  lc.clientId === targetId && !lc.sentAt
                    ? { ...lc, destinationOrganizationId: orgId }
                    : lc,
                );
              });
            }}
            sendBusyId={sendBusyId}
            onAddLabCase={() => void handleAddLabCase()}
            onSendLabCase={(lc, comment) => void handleSendLabCase(lc, comment)}
            onCommentError={showError}
            canInviteLab={canAccessOrganizations}
            onInviteLab={() => router.push('/organizations?action=invite-lab')}
          />
          ) : null}
          </div>
        </div>
      </div>
    </div>
  );
}
