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 staffing suggestion tool", () => {
  beforeEach(() => {
    vi.clearAllMocks();
  });

  it("prefers resources without a local holiday in staffing suggestions", async () => {
    const db = {
      project: {
        findUnique: vi.fn().mockResolvedValue({
          id: "project_1",
          name: "Holiday Project",
          shortCode: "HP",
          startDate: new Date("2026-01-06T00:00:00.000Z"),
          endDate: new Date("2026-01-06T00:00:00.000Z"),
        }),
        findFirst: vi.fn().mockResolvedValue(null),
      },
      assignment: {
        findMany: vi.fn().mockResolvedValue([]),
      },
      resource: {
        findMany: vi.fn().mockResolvedValue([
          {
            id: "res_by",
            displayName: "Bavaria",
            eid: "BY-1",
            fte: 1,
            lcrCents: 10000,
            chargeabilityTarget: 80,
            valueScore: 10,
            skills: [],
            availability: { monday: 8, tuesday: 8, wednesday: 8, thursday: 8, friday: 8 },
            countryId: "country_de",
            federalState: "BY",
            metroCityId: null,
            country: { code: "DE", name: "Deutschland" },
            metroCity: null,
            areaRole: { name: "Consultant" },
            chapter: "CGI",
          },
          {
            id: "res_hh",
            displayName: "Hamburg",
            eid: "HH-1",
            fte: 1,
            lcrCents: 10000,
            chargeabilityTarget: 80,
            valueScore: 10,
            skills: [],
            availability: { monday: 8, tuesday: 8, wednesday: 8, thursday: 8, friday: 8 },
            countryId: "country_de",
            federalState: "HH",
            metroCityId: null,
            country: { code: "DE", name: "Deutschland" },
            metroCity: null,
            areaRole: { name: "Consultant" },
            chapter: "CGI",
          },
        ]),
      },
    };
    const ctx = createToolContext(db, [
      PermissionKey.VIEW_PLANNING,
      PermissionKey.VIEW_COSTS,
    ]);

    const result = await executeTool(
      "get_staffing_suggestions",
      JSON.stringify({ projectId: "project_1", limit: 5 }),
      ctx,
    );

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

    expect(parsed.suggestions).toHaveLength(1);
    expect(parsed.suggestions[0]).toEqual(
      expect.objectContaining({ name: "Hamburg", availableHours: 8 }),
    );
    expect(db.project.findUnique).toHaveBeenCalledWith({
      where: { id: "project_1" },
      select: expect.objectContaining({
        id: true,
        shortCode: true,
        name: true,
        startDate: true,
        endDate: true,
      }),
    });
  });
});
