// ═══════════════════════════════════════════════════════════
// Resource: factory://dashboard/summary
// ═══════════════════════════════════════════════════════════

import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
import * as api from "../api-client.js";

export function registerDashboardResources(server: McpServer): void {
  server.resource(
    "dashboard-summary",
    "factory://dashboard/summary",
    {
      description: "High-level factory status: active pipelines, pending tasks, blockers, agent health",
      mimeType: "application/json",
    },
    async () => {
      try {
        const summary = await api.getDashboardSummary();
        return {
          contents: [
            {
              uri: "factory://dashboard/summary",
              mimeType: "application/json" as const,
              text: JSON.stringify(summary, null, 2),
            },
          ],
        };
      } catch (err) {
        const msg = err instanceof Error ? err.message : String(err);
        return {
          contents: [
            {
              uri: "factory://dashboard/summary",
              mimeType: "application/json" as const,
              text: JSON.stringify({
                error: msg,
                pipelines: { active: 0, paused: 0, completed: 0, failed: 0 },
                tasks: { pending: 0, inProgress: 0, blocked: 0, avgWaitTimeMinutes: 0, slaBreaches: 0, byPriority: {}, byType: {} },
                agents: { total: 0, active: 0, idle: 0, error: 0, offline: 0 },
                recentActivity: [],
                slaStatus: { onTrack: 0, warning: 0, breached: 0 },
              }, null, 2),
            },
          ],
        };
      }
    },
  );
}
