import type { ApiResult } from "./types";
import { authState } from "./auth";

const apiBaseUrl = import.meta.env.VITE_API_BASE_URL ?? "";

export function apiGet<T>(path: string): Promise<ApiResult<T>> {
  return apiRequest<T>("GET", path);
}

export function apiPost<T>(path: string, body: unknown): Promise<ApiResult<T>> {
  return apiRequest<T>("POST", path, body);
}

export function apiPut<T>(path: string, body: unknown): Promise<ApiResult<T>> {
  return apiRequest<T>("PUT", path, body);
}

export function apiPatch<T>(path: string, body: unknown): Promise<ApiResult<T>> {
  return apiRequest<T>("PATCH", path, body);
}

export function apiDelete<T>(path: string): Promise<ApiResult<T>> {
  return apiRequest<T>("DELETE", path);
}

async function apiRequest<T>(method: string, path: string, body?: unknown): Promise<ApiResult<T>> {
  try {
    const response = await fetch(`${apiBaseUrl}${path}`, {
      method,
      headers: {
        "Content-Type": "application/json",
        ...(authState.session?.accessToken ? { Authorization: `Bearer ${authState.session.accessToken}` } : {})
      },
      body: body === undefined ? undefined : JSON.stringify(body)
    });

    const text = await response.text();
    const data = text ? JSON.parse(text) : {};

    if (!response.ok) {
      return { ok: false, message: data.message ?? `HTTP ${response.status}` };
    }

    return { ok: true, data };
  } catch (error) {
    return {
      ok: false,
      message: error instanceof Error ? error.message : "Unbekannter Fehler"
    };
  }
}
