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

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

  it("returns stable assistant errors for holiday calendar and entry mutations", async () => {
    const cases = [
      {
        name: "invalid holiday calendar scope",
        toolName: "create_holiday_calendar",
        db: {
          country: {
            findUnique: vi.fn().mockResolvedValue({ id: "country_de", code: "DE", name: "Deutschland" }),
          },
          holidayCalendar: {
            findFirst: vi.fn().mockResolvedValue(null),
          },
        },
        payload: {
          name: "Ungueltiger Kalender",
          scopeType: "STATE",
          countryId: "country_de",
        },
        expected: "Holiday calendar scope is invalid.",
      },
      {
        name: "duplicate holiday calendar scope",
        toolName: "create_holiday_calendar",
        db: {
          country: {
            findUnique: vi.fn().mockResolvedValue({ id: "country_de", code: "DE", name: "Deutschland" }),
          },
          holidayCalendar: {
            findFirst: vi.fn().mockResolvedValue({ id: "cal_existing" }),
          },
        },
        payload: {
          name: "Bayern Feiertage",
          scopeType: "STATE",
          countryId: "country_de",
          stateCode: "BY",
        },
        expected: "A holiday calendar for this scope already exists.",
      },
      {
        name: "holiday calendar not found on delete",
        toolName: "delete_holiday_calendar",
        db: {
          holidayCalendar: {
            findUnique: vi.fn().mockResolvedValue(null),
          },
        },
        payload: { id: "cal_missing" },
        expected: "Holiday calendar not found with the given criteria.",
      },
      {
        name: "holiday calendar entry not found on delete",
        toolName: "delete_holiday_calendar_entry",
        db: {
          holidayCalendarEntry: {
            findUnique: vi.fn().mockResolvedValue(null),
          },
        },
        payload: { id: "entry_missing" },
        expected: "Holiday calendar entry not found with the given criteria.",
      },
      {
        name: "duplicate holiday calendar entry date",
        toolName: "create_holiday_calendar_entry",
        db: {
          holidayCalendar: {
            findUnique: vi.fn().mockResolvedValue({ id: "cal_by", name: "Bayern Feiertage" }),
          },
          holidayCalendarEntry: {
            findFirst: vi.fn().mockResolvedValue({ id: "entry_existing" }),
          },
        },
        payload: {
          holidayCalendarId: "cal_by",
          date: "2026-01-06",
          name: "Heilige Drei Koenige",
        },
        expected: "A holiday entry for this calendar and date already exists.",
      },
    ] as const;

    for (const testCase of cases) {
      const result = await executeTool(
        testCase.toolName,
        JSON.stringify(testCase.payload),
        createToolContext(testCase.db),
      );

      expect(JSON.parse(result.content)).toEqual({
        error: testCase.expected,
      });
    }
  });
});
