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

/**
 * Chair approval center (INV-G10) — aggregates every pending human-gate item
 * (halacha approvals, missing precedents, unapplied feedback, QA-failed cases)
 * so nothing Dafna must approve is forgotten.
 *
 * Hand-typed (not from the generated types.ts) because /api/chair/pending is a
 * new endpoint; switch to the generated type after the next `npm run api:types`.
 */
export type ApprovalSeverity = "high" | "medium" | "low" | "ok";

export type ApprovalSample = { text: string; source: string; href?: string | null };

export type ApprovalCategory = {
  key: string;
  label: string;
  description: string;
  count: number;
  severity: ApprovalSeverity;
  href: string | null;
  oldest_at?: string | null;
  sample?: ApprovalSample[];
  extra?: { total: number; reviewed: number };
};

export type PendingApprovals = {
  total_pending: number;
  generated_at: string;
  categories: ApprovalCategory[];
};

export function usePendingApprovals() {
  return useQuery({
    queryKey: ["chair", "pending"],
    queryFn: () => apiRequest<PendingApprovals>("/api/chair/pending"),
    refetchInterval: 60_000,
    staleTime: 30_000,
  });
}
