// Central API client — swap BASE_URL and add auth headers when backend arrives.
export const API_BASE = import.meta.env.VITE_API_BASE ?? "/api";

export class ApiError extends Error {
  status: number;
  constructor(message: string, status: number) {
    super(message);
    this.status = status;
  }
}

export async function apiFetch<T>(path: string, init?: RequestInit): Promise<T> {
  const res = await fetch(`${API_BASE}${path}`, {
    ...init,
    headers: {
      "Content-Type": "application/json",
      ...(init?.headers ?? {}),
    },
    credentials: "include",
  });
  if (!res.ok) throw new ApiError(await res.text(), res.status);
  return res.json() as Promise<T>;
}

// Feature service modules re-export typed functions built on apiFetch.
// For now, features import from src/features/_mock/data.ts.
