import { beforeEach, describe, expect, it, vi } from "vitest";

vi.mock("@capakraken/application", async (importOriginal) => {
  const actual = await importOriginal<typeof import("@capakraken/application")>();
  return {
    ...actual,
    getDashboardBudgetForecast: vi.fn().mockResolvedValue([]),
    listAssignmentBookings: vi.fn().mockResolvedValue([]),
  };
});

vi.mock("../lib/audit.js", () => ({
  createAuditEntry: vi.fn().mockResolvedValue(undefined),
}));

import { executeTool } from "../router/assistant-tools.js";
import { createToolContext } from "./assistant-tools-holiday-capacity-test-helpers.js";

describe("assistant holiday-aware scenario simulation tool", () => {
  beforeEach(() => {
    vi.clearAllMocks();
  });

  it("keeps scenario simulation flat when a proposed change falls on a local holiday", async () => {
    const db = {
      project: {
        findUnique: vi.fn().mockResolvedValue({
          id: "project_1",
          name: "Holiday Project",
          budgetCents: 500_000,
          startDate: new Date("2026-01-01T00:00:00.000Z"),
          endDate: new Date("2026-01-31T00:00:00.000Z"),
        }),
      },
      assignment: {
        findMany: vi.fn().mockResolvedValue([
          {
            id: "assignment_1",
            resourceId: "res_1",
            hoursPerDay: 8,
            startDate: new Date("2026-01-05T00:00:00.000Z"),
            endDate: new Date("2026-01-05T00:00:00.000Z"),
            status: "CONFIRMED",
            resource: {
              id: "res_1",
              displayName: "Bruce Banner",
              lcrCents: 100,
              availability: { monday: 8, tuesday: 8, wednesday: 8, thursday: 8, friday: 8 },
              chargeabilityTarget: 80,
              countryId: "country_de",
              federalState: "BY",
              metroCityId: null,
              country: { code: "DE", dailyWorkingHours: 8 },
              metroCity: null,
            },
          },
        ]),
      },
      resource: {
        findMany: vi.fn().mockResolvedValue([
          {
            id: "res_1",
            displayName: "Bruce Banner",
            lcrCents: 100,
            availability: { monday: 8, tuesday: 8, wednesday: 8, thursday: 8, friday: 8 },
            chargeabilityTarget: 80,
            countryId: "country_de",
            federalState: "BY",
            metroCityId: null,
            country: { code: "DE", dailyWorkingHours: 8 },
            metroCity: null,
          },
        ]),
      },
    };
    const ctx = createToolContext(db, ["manageAllocations"]);

    const result = await executeTool(
      "simulate_scenario",
      JSON.stringify({
        projectId: "project_1",
        changes: [
          {
            resourceId: "res_1",
            startDate: "2026-01-06",
            endDate: "2026-01-06",
            hoursPerDay: 8,
          },
        ],
      }),
      ctx,
    );

    const parsed = JSON.parse(result.content) as {
      baseline: { totalHours: number; totalCostCents: number };
      scenario: { totalHours: number; totalCostCents: number };
      delta: { hours: number; costCents: number };
    };

    expect(parsed.baseline).toEqual(
      expect.objectContaining({
        totalHours: 8,
        totalCostCents: 800,
      }),
    );
    expect(parsed.scenario).toEqual(
      expect.objectContaining({
        totalHours: 8,
        totalCostCents: 800,
      }),
    );
    expect(parsed.delta).toEqual(
      expect.objectContaining({
        hours: 0,
        costCents: 0,
      }),
    );
  });
});
