import { useQuery } from "@tanstack/react-query";
import { apiFetch } from "./client";
import type { CostResult } from "@/types/api";

export interface CostQueryParams {
  group_by?: "provider" | "model" | "department";
  start?: string;
  end?: string;
}

function buildQueryString(params: Record<string, string | undefined>): string {
  const q = new URLSearchParams();
  for (const [k, v] of Object.entries(params)) {
    if (v) q.set(k, v);
  }
  const s = q.toString();
  return s ? `?${s}` : "";
}

export function useCosts(params: CostQueryParams = {}, refetchInterval?: number) {
  const qs = buildQueryString(params as Record<string, string | undefined>);
  return useQuery({
    queryKey: ["costs", params],
    queryFn: () => apiFetch<CostResult>(`/v1/admin/costs${qs}`),
    refetchInterval,
  });
}
