import axios, { type AxiosInstance } from "axios"; /** Base API URL – from env or fallback for local dev */ const getBaseUrl = (): string => typeof window !== "undefined" ? process.env.NEXT_PUBLIC_API_URL || "" : process.env.NEXT_PUBLIC_API_URL || process.env.API_URL || "http://localhost:8000"; /** API client instance */ function createApiClient(): AxiosInstance { const baseURL = getBaseUrl(); return axios.create({ baseURL: baseURL ? `${baseURL.replace(/\/$/, "")}/api/v1` : "/api/v1", headers: { "Content-Type": "application/json" }, timeout: 60_000, }); } const api = createApiClient(); // ─── Types (aligned with backend schemas) ───────────────────────────────────── export interface GenerationRequest { prompt: string; lyrics?: string; duration?: number; style?: string; voice_preset?: string; vocal_volume?: number; instrumental_volume?: number; } /** Prompt analysis from backend (nested in metadata) */ export interface PromptAnalysis { original_prompt?: string; style?: string; tempo?: number | string; mood?: string; instrumentation?: string[]; lyrics?: string; duration_hint?: number; enriched_prompt?: string; } export interface GenerationMetadata { prompt?: string; analysis?: PromptAnalysis; } export interface GenerationResponse { id: string; status: "pending" | "processing" | "completed" | "failed"; prompt: string; audio_path?: string | null; metadata?: GenerationMetadata | null; processing_time_seconds?: number | null; error_message?: string | null; created_at?: string | null; completed_at?: string | null; } export interface GenerationListResponse { items: GenerationResponse[]; total: number; page: number; page_size: number; } // ─── API methods ───────────────────────────────────────────────────────────── export const generationsApi = { async create(data: GenerationRequest): Promise { const { data: res } = await api.post("/generations/", data); return res; }, async get(id: string): Promise { const { data } = await api.get(`/generations/${id}`); return data; }, async list(page: number = 1, pageSize: number = 20): Promise { const { data } = await api.get("/generations/", { params: { page, page_size: pageSize }, }); return data; }, };