import { queryOptions } from "@tanstack/react-query";
import { jsonQuery } from "../apiAdapter";

export interface AiUsage {
  configured: boolean;
  month_spend_usd: number;
  budget_usd: number;
  remaining_usd: number;
}

export interface ExtractedInvoice {
  supplier_name: string;
  invoice_number: string | null;
  amount: number;
  currency: string;
  vat_rate: number;
  issue_date: string | null;
  due_date: string | null;
  description: string | null;
}

export interface StoredChatMessage {
  role: "user" | "assistant";
  content: string;
  created_at: string;
}

export const aiUsageOptions = () =>
  queryOptions({
    queryKey: ["ai", "usage"],
    queryFn: () => jsonQuery<AiUsage>("/api/admin/ai/usage"),
    staleTime: 30_000,
  });

export interface Conversation {
  id: number;
  title: string;
  updated_at: string;
}

export const aiConversationsOptions = () =>
  queryOptions({
    queryKey: ["ai", "conversations"],
    queryFn: () =>
      jsonQuery<{ conversations: Conversation[] }>(
        "/api/admin/ai/conversations",
      ),
    staleTime: 30_000,
  });

export const aiConversationMessagesOptions = (id: number) =>
  queryOptions({
    queryKey: ["ai", "conversation", id, "messages"],
    queryFn: () =>
      jsonQuery<{ messages: StoredChatMessage[] }>(
        `/api/admin/ai/conversations/${id}/messages`,
      ),
    // Re-read the DB on each mount/selection (no stale-cache resurrection).
    staleTime: Infinity,
    gcTime: 0,
  });
