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

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-test-helpers.js";

describe("assistant holiday calendar mutation tools - guards", () => {
  beforeEach(() => {
    vi.clearAllMocks();
  });

  it("rejects holiday calendar mutations for non-admin assistant users", async () => {
    const ctx = createToolContext({}, [], SystemRole.MANAGER);
    const result = await executeTool(
      "create_holiday_calendar",
      JSON.stringify({
        name: "Hamburg Feiertage",
        scopeType: "STATE",
        countryId: "country_de",
        stateCode: "HH",
      }),
      ctx,
    );

    expect(JSON.parse(result.content)).toEqual(
      expect.objectContaining({
        error: "You do not have permission to perform this action.",
      }),
    );
  });

  it("returns a stable error when a holiday calendar scope already exists", async () => {
    const ctx = createToolContext({
      country: {
        findUnique: vi.fn().mockResolvedValue({ id: "country_de", name: "Germany" }),
      },
      holidayCalendar: {
        findFirst: vi.fn().mockResolvedValue({ id: "cal_existing" }),
      },
    });

    const result = await executeTool(
      "create_holiday_calendar",
      JSON.stringify({ name: "Germany National", scopeType: "COUNTRY", countryId: "country_de" }),
      ctx,
    );

    expect(JSON.parse(result.content)).toEqual({
      error: "A holiday calendar for this scope already exists.",
    });
  });
});
