/**
 * Methodology settings hooks — view and edit golden ratios,
 * discussion rules, and content checklists.
 */

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

// ── Types ────────────────────────────────────────────────────────

export type MethodologyItem<T = unknown> = {
  value: T;
  is_override: boolean;
  updated_at: string | null;
};

export type MethodologyResponse<T = unknown> = {
  items: Record<string, MethodologyItem<T>>;
};

/** Golden ratio per section: [min%, max%] */
export type GoldenRatios = Record<string, [number, number]>;

// ── Query Keys ───────────────────────────────────────────────────

export const methodologyKeys = {
  all: ["methodology"] as const,
  category: (cat: string) => [...methodologyKeys.all, cat] as const,
};

// ── Hooks ────────────────────────────────────────────────────────

export function useMethodology<T = unknown>(category: string) {
  return useQuery({
    queryKey: methodologyKeys.category(category),
    queryFn: ({ signal }) =>
      apiRequest<MethodologyResponse<T>>(
        `/api/methodology/${category}`,
        { signal },
      ),
    staleTime: 30_000,
  });
}

export function useUpdateMethodology(category: string) {
  const qc = useQueryClient();
  return useMutation({
    mutationFn: ({ key, value }: { key: string; value: unknown }) =>
      apiRequest<{ key: string; value: unknown; is_override: boolean }>(
        `/api/methodology/${category}/${key}`,
        { method: "PUT", body: { value } },
      ),
    onSuccess: () => {
      qc.invalidateQueries({ queryKey: methodologyKeys.category(category) });
    },
  });
}

export function useResetMethodology(category: string) {
  const qc = useQueryClient();
  return useMutation({
    mutationFn: (key: string) =>
      apiRequest<{ key: string; value: unknown; is_override: boolean }>(
        `/api/methodology/${category}/${key}`,
        { method: "DELETE" },
      ),
    onSuccess: () => {
      qc.invalidateQueries({ queryKey: methodologyKeys.category(category) });
    },
  });
}
