/**
 * Typed API client (design D12).
 *
 * Types come from `schema.d.ts`, which is generated from the API's OpenAPI document — do
 * not hand-write request or response types anywhere in the frontend. If a Pydantic model
 * changes, this file's consumers stop compiling, which is the point.
 *
 * Server-side calls must forward the session cookie explicitly: Next.js server components
 * do not carry browser cookies automatically.
 */
import createClient from "openapi-fetch";

import type { paths } from "./schema";

// Server-rendered requests talk to the API container directly, skipping the proxy hop.
// Browser requests use a relative path so they stay same-origin (design D4).
const baseUrl =
  typeof window === "undefined"
    ? (process.env.INTERNAL_API_URL ?? "http://api:8000")
    : "";

export const api = createClient<paths>({ baseUrl });

export type ApiPaths = paths;
