// frontend/lib/hooks/useApprovals.ts
"use client";
import useSWR from "swr";
import type { ApprovalQueue, Autonomy } from "@/lib/types";
import { approvalsApi } from "@/lib/approvals/api";

export function useApprovals() {
  const { data, error, isLoading, mutate } = useSWR<ApprovalQueue>(
    "approvals",
    () => approvalsApi.queue(),
    { revalidateOnFocus: false },
  );

  const setAutonomy = async (level: Autonomy) => {
    await approvalsApi.setAutonomy(level); // 변경 즉시 큐 재구성
    await mutate(); // GET /approvals 가 derive_queue 반영
  };
  const approve = async (id: string) => {
    await approvalsApi.approve(id);
    await mutate();
  };
  const undo = async (id: string) => {
    await approvalsApi.undo(id);
    await mutate();
  };
  const approveAll = async () => {
    await approvalsApi.approveAll();
    await mutate();
  };
  return { data, error, isLoading, setAutonomy, approve, undo, approveAll, reload: mutate };
}
