"use client";

import { useQuery } from "@tanstack/react-query";
import { apiFetch } from "@/lib/api";
import type { QuotaStatus } from "@/types/trade-in";

const BASE = "/api/v1/trade-in";

/**
 * GET /api/v1/trade-in/quota
 * Fetches the monthly evaluation quota status for the current session/account.
 * Fail-open: if the query errors, the UI must NOT block the user (treat as unlimited).
 *
 * #800: `retry:false` — на 401/протухшей сессии quota НЕ должна ре-стучаться в бэк.
 * `refetchOnMount/Reconnect/WindowFocus:false` — иначе errored quota ре-фетчит на
 * каждый ремаунт владельца → вклад в retry-storm. Освежается явным
 * `invalidateQueries(["trade-in","quota"])` после успешной оценки (см. page.tsx).
 */
export function useQuota() {
  return useQuery<QuotaStatus>({
    queryKey: ["trade-in", "quota"],
    queryFn: () => apiFetch<QuotaStatus>(`${BASE}/quota`),
    staleTime: 30_000,
    retry: false,
    refetchOnMount: false,
    refetchOnReconnect: false,
    refetchOnWindowFocus: false,
  });
}
