export type Project = { id: number; name: string; description: string };

export type ProjectDashboardSummary = {
  project_id: number;
  pending_inbox_count: number;
  open_task_count: number;
  recent_note_count: number;
  recent_session_count: number;
};

export function createApi(apiBase: string, token?: string) {
  async function request<T>(path: string): Promise<T> {
    const response = await fetch(`${apiBase}/api${path}`, {
      headers: token ? { Authorization: `Bearer ${token}` } : {},
    });
    if (!response.ok) throw new Error(`API request failed: ${response.status}`);
    return response.json() as Promise<T>;
  }

  return {
    getProjects: () => request<Project[]>('/projects'),
    getProjectDashboard: (projectID: number) =>
      request<ProjectDashboardSummary>(`/projects/${projectID}/dashboard`),
  };
}
