/**
 * Dashboard API Client
 * Handles dashboard summary data fetching
 */

import { apiGet } from "./client";

/* ------------------------------------------------------------------ */
/*  Type definitions matching backend DashboardSummaryDto              */
/* ------------------------------------------------------------------ */

export interface DashboardMetrics {
  activeAgents: number;
  tasksCompleted: number;
  totalTasks: number;
  tasksInProgress: number;
  activeProjects: number;
  errorRate: number;
}

export interface RecentActivity {
  id: string;
  action: string;
  entityType: string;
  entityId: string;
  details: Record<string, unknown> | null;
  userId: string;
  createdAt: string;
}

export interface ActiveJobStep {
  id: string;
  name: string;
  status: string;
  phase: string;
}

export interface ActiveJob {
  id: string;
  type: string;
  status: string;
  progressPercent: number;
  createdAt: string;
  updatedAt: string;
  steps: ActiveJobStep[];
}

export interface TokenBudgetEntry {
  model: string;
  used: number;
  limit: number;
}

export interface DashboardSummaryResponse {
  metrics: DashboardMetrics;
  recentActivity: RecentActivity[];
  activeJobs: ActiveJob[];
  tokenBudget: TokenBudgetEntry[];
}

/* ------------------------------------------------------------------ */
/*  API function                                                       */
/* ------------------------------------------------------------------ */

/**
 * Fetch dashboard summary data for the given workspace.
 *
 * @param workspaceId - Optional workspace ID sent via X-Workspace-Id header
 */
export async function fetchDashboardSummary(
  workspaceId?: string
): Promise<DashboardSummaryResponse> {
  return apiGet<DashboardSummaryResponse>("/api/dashboard/summary", workspaceId ?? undefined);
}
