import { beforeEach, describe, expect, it, vi } from "vitest";
import { SystemRole } from "@capakraken/shared";

import {
  createToolContext,
  executeTool,
  getDashboardProjectHealth,
} from "./assistant-tools-dashboard-test-helpers.js";

describe("assistant dashboard tools project health", () => {
  beforeEach(() => {
    vi.clearAllMocks();
  });

  it("routes project health reads through the dashboard router path", async () => {
    vi.mocked(getDashboardProjectHealth).mockResolvedValue([
      {
        id: "project_1",
        projectName: "Apollo",
        shortCode: "APL",
        status: "ACTIVE",
        clientId: "client_1",
        clientName: "Acme Mobility",
        budgetHealth: 74,
        staffingHealth: 88,
        timelineHealth: 63,
        compositeScore: 75,
        budgetCents: 250_000,
        spentCents: 180_000,
        remainingBudgetCents: 70_000,
        budgetUtilizationPercent: 72,
        demandHeadcountTotal: 6,
        demandHeadcountFilled: 5,
        demandHeadcountOpen: 1,
        demandRequirementCount: 3,
        plannedEndDate: new Date("2026-08-31T00:00:00.000Z"),
        daysUntilEndDate: 45,
        timelineStatus: "ON_TRACK",
        calendarLocations: [
          {
            countryCode: "DE",
            countryName: "Germany",
            federalState: "BY",
            metroCityName: "Munich",
            assignmentCount: 3,
            spentCents: 120_000,
          },
        ],
        derivation: {
          periodStart: "2026-08-01",
          periodEnd: "2026-08-31",
          calendarContextCount: 1,
          holidayAwareAssignmentCount: 3,
          fallbackAssignmentCount: 0,
          baseSpentCents: 190_000,
          adjustedSpentCents: 180_000,
          publicHolidayDayEquivalent: 1,
          publicHolidayCostDeductionCents: 6_000,
          absenceDayEquivalent: 0.5,
          absenceCostDeductionCents: 4_000,
        },
      },
    ]);

    const ctx = createToolContext({}, { userRole: SystemRole.CONTROLLER });
    const result = await executeTool("get_project_health", "{}", ctx);

    expect(getDashboardProjectHealth).toHaveBeenCalledTimes(1);
    expect(JSON.parse(result.content)).toEqual({
      projects: [
        {
          projectId: "project_1",
          projectName: "Apollo",
          shortCode: "APL",
          status: "ACTIVE",
          overall: 75,
          budget: 74,
          staffing: 88,
          timeline: 63,
          rating: "at_risk",
          budgetBasis: {
            budgetCents: 250_000,
            spentCents: 180_000,
            remainingBudgetCents: 70_000,
            budgetUtilizationPercent: 72,
            calendarLocations: [
              {
                countryCode: "DE",
                countryName: "Germany",
                federalState: "BY",
                metroCityName: "Munich",
                assignmentCount: 3,
                spentCents: 120_000,
              },
            ],
            derivation: {
              periodStart: "2026-08-01",
              periodEnd: "2026-08-31",
              calendarContextCount: 1,
              holidayAwareAssignmentCount: 3,
              fallbackAssignmentCount: 0,
              baseSpentCents: 190_000,
              adjustedSpentCents: 180_000,
              publicHolidayDayEquivalent: 1,
              publicHolidayCostDeductionCents: 6_000,
              absenceDayEquivalent: 0.5,
              absenceCostDeductionCents: 4_000,
            },
          },
          staffingBasis: {
            demandHeadcountTotal: 6,
            demandHeadcountFilled: 5,
            demandHeadcountOpen: 1,
            demandRequirementCount: 3,
          },
          timelineBasis: {
            plannedEndDate: "2026-08-31T00:00:00.000Z",
            daysUntilEndDate: 45,
            timelineStatus: "ON_TRACK",
          },
          context: {
            clientId: "client_1",
            clientName: "Acme Mobility",
          },
        },
      ],
      summary: {
        healthy: 0,
        atRisk: 1,
        critical: 0,
      },
    });
  });
});
