"use client";

import { useMutation, useQuery } from "@tanstack/react-query";

import { apiFetch } from "@/lib/api";

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

export interface LandingStats {
  zk_total: number;
  deals_total: number;
  price_coverage_pct: number;
  mapping_coverage_pct: number;
  last_data_update: string;
  paradox: string;
  /** true when DB is unavailable or all-zero — frontend shows skeleton */
  stale?: boolean;
}

export interface PilotRequestPayload {
  name: string;
  phone?: string;
  email?: string;
  company?: string;
  message?: string;
  source: string;
}

export interface PilotRequestResponse {
  /** UUID returned as string via CAST(id AS text) */
  id: string;
  status: string;
  created_at: string;
}

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

export function useLandingStats() {
  return useQuery<LandingStats>({
    queryKey: ["landing", "stats"],
    queryFn: () => apiFetch<LandingStats>("/api/v1/landing/stats"),
    staleTime: 60_000,
  });
}

export function useSubmitPilotRequest() {
  return useMutation<PilotRequestResponse, Error, PilotRequestPayload>({
    mutationFn: (payload: PilotRequestPayload) =>
      apiFetch<PilotRequestResponse>("/api/v1/pilot/request", {
        method: "POST",
        body: JSON.stringify(payload),
      }),
  });
}
