'use client';

import { useMemo } from 'react';
import { FdiToothChart } from '@/components/ui/treatment/FdiToothChart';
import { toothRegionColors } from '@/components/treatment/prosthesisTree';
import { prosthesisTypeColor, splitProsthesisGroupCode } from '@/components/treatment/prosthesisTypeDisplay';
import type { ProsthesisCatalogEntry } from '@/types/treatment-catalog';
import type { FdiToothId } from '@/types/treatment';

export interface CaseToothChartDetail {
  teeth: string[];
}

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

interface CaseToothChartPanelProps {
  details: CaseToothChartDetail[];
  /** Prosthesis mapping from case tasks or toothProsthesis rows. */
  prosthesisRows: CaseToothChartProsthesisRow[];
  /** Teeth in multi-tooth connected bridges (link dots on the chart). */
  connectedTeeth?: ReadonlySet<FdiToothId>;
  prosthesisCatalog?: readonly ProsthesisCatalogEntry[];
  scale?: number;
  compact?: boolean;
  className?: string;
}

/** Read-only FDI chart for lab case detail — prosthesis-type glow + connected bridge dots. */
export function CaseToothChartPanel({
  details,
  prosthesisRows,
  connectedTeeth,
  prosthesisCatalog,
  scale = 1,
  compact = true,
  className = '',
}: CaseToothChartPanelProps) {
  const selected = useMemo(() => {
    const set = new Set<FdiToothId>();
    for (const detail of details) {
      for (const tooth of detail.teeth) set.add(tooth as FdiToothId);
    }
    return set;
  }, [details]);

  const regionColors = useMemo(() => {
    const flat = prosthesisRows.flatMap((row) =>
      splitProsthesisGroupCode(row.prosthesisTypeCode).flatMap((code) =>
        row.teeth.map((tooth) => ({ tooth, prosthesisTypeCode: code })),
      ),
    );
    if (prosthesisCatalog?.length) {
      return toothRegionColors(flat, prosthesisCatalog);
    }
    const crown: Partial<Record<FdiToothId, string>> = {};
    prosthesisRows.forEach((row, index) => {
      const color = prosthesisTypeColor(row.prosthesisTypeCode, index);
      for (const tooth of row.teeth) {
        crown[tooth as FdiToothId] = color;
      }
    });
    return { crown, root: {} as Partial<Record<FdiToothId, string>> };
  }, [prosthesisRows, prosthesisCatalog]);

  const resolvedConnected = useMemo(() => {
    if (connectedTeeth) return connectedTeeth;
    const set = new Set<FdiToothId>();
    for (const row of prosthesisRows) {
      if (!row.connected || row.teeth.length < 2) continue;
      for (const tooth of row.teeth) set.add(tooth as FdiToothId);
    }
    return set;
  }, [connectedTeeth, prosthesisRows]);

  if (selected.size === 0) return null;

  return (
    <FdiToothChart
      selected={selected}
      readOnly
      scale={scale}
      crownColors={regionColors.crown}
      rootColors={regionColors.root}
      connectedTeeth={resolvedConnected.size > 0 ? resolvedConnected : undefined}
      compact={compact}
      className={className}
    />
  );
}
