/**
 * Citations domain — precedents CITED inside a case's signed decision.
 * Powers the case-page "פסיקה שצוטטה בהחלטה" panel.
 */

import { useQuery } from "@tanstack/react-query";
import { apiRequest } from "./client";

export type LinkedCitation = {
  citation: string;
  cited_id: string;
  /** May be null/empty — the panel guards with `c.case_name && …`. */
  case_name: string | null;
  court: string;
  precedent_level: string;
};

export type MissingCitation = {
  citation: string;
  flagged: boolean;
};

export type CaseCitations = {
  in_library: boolean;
  case_law_id?: string;
  linked: LinkedCitation[];
  missing: MissingCitation[];
};

export function useCaseCitations(caseNumber: string | undefined) {
  return useQuery({
    queryKey: ["case-citations", caseNumber ?? ""],
    queryFn: ({ signal }) =>
      apiRequest<CaseCitations>(`/api/cases/${caseNumber}/citations`, { signal }),
    enabled: Boolean(caseNumber),
    staleTime: 10_000,
  });
}
