"use client"; import { useMutation, useQuery } from "@tanstack/react-query"; import { apiFetch } from "./api"; import type { AggregatedEstimate, HouseInfoForEstimate, IMVBenchmarkResponse, TradeInEstimateInput, } from "@/types/trade-in"; const BASE = "/api/v1/trade-in"; /** * POST /api/v1/trade-in/estimate * Sends apartment parameters and returns AggregatedEstimate (mock data for now). */ export function useEstimateMutation() { return useMutation({ mutationFn: (input) => apiFetch(`${BASE}/estimate`, { method: "POST", body: JSON.stringify(input), }), }); } /** * GET /api/v1/trade-in/estimate/{id} * Fetches a previously computed estimate by UUID (for shareable links / PDF). */ export function useEstimate(estimate_id: string | null) { return useQuery({ queryKey: ["trade-in", "estimate", estimate_id], queryFn: () => apiFetch(`${BASE}/estimate/${estimate_id}`), enabled: estimate_id !== null && estimate_id.length > 0, staleTime: 10 * 60_000, // 10 min — estimates expire at expires_at anyway }); } /** * GET /api/v1/trade-in/estimate/{id}/houses * Houses в радиусе 500m от target адреса estimate (Stage 2c data). */ export function useEstimateHouses(estimate_id: string | null) { return useQuery({ queryKey: ["trade-in", "estimate", estimate_id, "houses"], queryFn: () => apiFetch(`${BASE}/estimate/${estimate_id}/houses`), enabled: estimate_id !== null && estimate_id.length > 0, staleTime: 10 * 60_000, }); } /** * GET /api/v1/trade-in/estimate/{id}/imv-benchmark * Avito IMV benchmark для UI badge (Stage 3 IMV cache lookup). */ export function useEstimateImvBenchmark(estimate_id: string | null) { return useQuery({ queryKey: ["trade-in", "estimate", estimate_id, "imv-benchmark"], queryFn: () => apiFetch(`${BASE}/estimate/${estimate_id}/imv-benchmark`), enabled: estimate_id !== null && estimate_id.length > 0, staleTime: 10 * 60_000, }); }