import type { AuthResponse, BlogPost, BlogPostDetail, ChartData, CostResult, Destination, FAQ, ProfileStats, SearchResult, Stats, SyncedUserPlan, Testimonial, Tool, Visa, } from "./types"; const API_BASE = process.env.NEXT_PUBLIC_API_URL || "http://localhost:8000/api/v1"; // Fallback data when API is unavailable (build time / offline) const FALLBACK_STATS: Stats = { countries: 195, avg_cost: 42, satisfaction: 87, total_nomads: "35.6M", active_today: 127 }; const FALLBACK_TICKER = { messages: ["🌴 小林 刚刚抵达清迈", "💻 Marco 在里斯本完成了 Sprint", "✈️ 今日新增 127 位游民出发"] }; async function fetchAPI(path: string, options?: RequestInit): Promise { const isMutation = options?.method && options.method !== "GET"; try { const res = await fetch(`${API_BASE}${path}`, { ...options, headers: { "Content-Type": "application/json", ...options?.headers }, ...(isMutation ? { cache: "no-store" } : { next: { revalidate: 60 } }), }); if (!res.ok) throw new Error(`API error: ${res.status}`); return res.json(); } catch (err) { if (isMutation) throw err; throw err; } } async function fetchWithFallback(path: string, fallback: T): Promise { try { return await fetchAPI(path); } catch { return fallback; } } export const api = { getStats: () => fetchWithFallback("/stats", FALLBACK_STATS), getTicker: () => fetchWithFallback("/ticker", FALLBACK_TICKER), getDestinations: (params?: { region?: string; search?: string; sort?: string }) => { const q = new URLSearchParams(); if (params?.region) q.set("region", params.region); if (params?.search) q.set("search", params.search); if (params?.sort) q.set("sort", params.sort); const qs = q.toString(); return fetchAPI(`/destinations${qs ? `?${qs}` : ""}`); }, getDestination: (slug: string) => fetchAPI(`/destinations/${slug}`), compareDestinations: (slugs: string[]) => fetchAPI("/destinations/compare", { method: "POST", body: JSON.stringify({ slugs }), }), getVisas: () => fetchWithFallback("/visas", []), getFaqs: () => fetchWithFallback("/faqs", []), getTestimonials: () => fetchWithFallback("/testimonials", []), getTools: () => fetchWithFallback("/tools", []), getBlog: () => fetchWithFallback("/blog", []), getBlogPost: (slug: string) => fetchAPI(`/blog/${slug}`), subscribe: (email: string) => fetchAPI<{ success: boolean; message: string }>("/subscribe", { method: "POST", body: JSON.stringify({ email }), }), calculateCost: (data: { destination_slug: string; housing: string; months: number }) => fetchAPI("/calculator", { method: "POST", body: JSON.stringify(data) }), getChartCost: () => fetchWithFallback("/charts/cost", { labels: [], datasets: [{ data: [] }] }), getChartSpeed: () => fetchWithFallback("/charts/speed", { labels: [], datasets: [{ data: [] }] }), getChartGrowth: () => fetchWithFallback("/charts/growth", { labels: [], datasets: [{ data: [] }] }), getChartRadar: () => fetchWithFallback("/charts/radar", { labels: [], datasets: [] }), // Auth login: (email: string, password: string) => fetchAPI("/auth/login", { method: "POST", body: JSON.stringify({ email, password }) }), register: (email: string, password: string, name: string) => fetchAPI("/auth/register", { method: "POST", body: JSON.stringify({ email, password, name }) }), demoLogin: () => fetchAPI("/auth/demo"), getFavorites: (token: string) => fetchAPI<{ slugs: string[] }>("/auth/favorites", { headers: { Authorization: `Bearer ${token}` } }), toggleFavorite: (slug: string, token: string) => fetchAPI<{ slugs: string[] }>("/auth/favorites", { method: "POST", headers: { Authorization: `Bearer ${token}` }, body: JSON.stringify({ destination_slug: slug }), }), getFavoriteDestinations: (token: string) => fetchAPI("/auth/favorites/detail", { headers: { Authorization: `Bearer ${token}` }, }), getProfileStats: (token: string) => fetchAPI("/auth/profile/stats", { headers: { Authorization: `Bearer ${token}` }, }), getPlan: (token: string) => fetchAPI("/auth/plan", { headers: { Authorization: `Bearer ${token}` }, }), savePlan: (token: string, plan: SyncedUserPlan) => fetchAPI("/auth/plan", { method: "PUT", headers: { Authorization: `Bearer ${token}` }, body: JSON.stringify(plan), }), search: (q: string) => fetchAPI(`/search?q=${encodeURIComponent(q)}`), };