import { PermissionKey } from "@capakraken/shared";
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 capacity tool", () => {
  beforeEach(() => {
    vi.clearAllMocks();
  });

  it("finds capacity with local holidays respected", async () => {
    const db = {
      resource: {
        findMany: vi.fn().mockResolvedValue([
          {
            id: "res_by",
            displayName: "Bavaria",
            eid: "BY-1",
            fte: 1,
            availability: { monday: 8, tuesday: 8, wednesday: 8, thursday: 8, friday: 8 },
            countryId: "country_de",
            federalState: "BY",
            metroCityId: null,
            country: { code: "DE" },
            metroCity: null,
            areaRole: { name: "Consultant" },
            chapter: "CGI",
            assignments: [],
          },
          {
            id: "res_hh",
            displayName: "Hamburg",
            eid: "HH-1",
            fte: 1,
            availability: { monday: 8, tuesday: 8, wednesday: 8, thursday: 8, friday: 8 },
            countryId: "country_de",
            federalState: "HH",
            metroCityId: null,
            country: { code: "DE" },
            metroCity: null,
            areaRole: { name: "Consultant" },
            chapter: "CGI",
            assignments: [],
          },
        ]),
      },
      vacation: {
        findMany: vi.fn().mockResolvedValue([]),
      },
    };
    const ctx = createToolContext(db, [PermissionKey.VIEW_PLANNING]);

    const result = await executeTool(
      "find_capacity",
      JSON.stringify({ startDate: "2026-01-06", endDate: "2026-01-06", minHoursPerDay: 1 }),
      ctx,
    );

    const parsed = JSON.parse(result.content) as {
      results: Array<{ name: string; availableHours: number; availableHoursPerDay: number }>;
    };

    expect(parsed.results).toHaveLength(1);
    expect(parsed.results[0]).toEqual(
      expect.objectContaining({ name: "Hamburg", availableHours: 8, availableHoursPerDay: 8 }),
    );
  });
});
