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 entry mutation tools - errors", () => {
  beforeEach(() => {
    vi.clearAllMocks();
  });

  it("returns a stable error when a holiday entry calendar cannot be found", async () => {
    const ctx = createToolContext({
      holidayCalendar: {
        findUnique: vi.fn().mockResolvedValue(null),
      },
    });

    const result = await executeTool(
      "create_holiday_calendar_entry",
      JSON.stringify({
        holidayCalendarId: "cal_missing",
        date: "2026-01-01",
        name: "New Year",
      }),
      ctx,
    );

    expect(JSON.parse(result.content)).toEqual({
      error: "Holiday calendar not found with the given criteria.",
    });
  });

  it("returns a stable error when a holiday entry date conflicts during update", async () => {
    const ctx = createToolContext({
      holidayCalendarEntry: {
        findUnique: vi.fn().mockResolvedValue({
          id: "entry_1",
          name: "New Year",
          date: new Date("2026-01-01T00:00:00.000Z"),
          holidayCalendarId: "cal_de",
        }),
        findFirst: vi.fn().mockResolvedValue({ id: "entry_conflict" }),
      },
    });

    const result = await executeTool(
      "update_holiday_calendar_entry",
      JSON.stringify({
        id: "entry_1",
        data: { date: "2026-01-02" },
      }),
      ctx,
    );

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

  it("returns a stable error when deleting a missing holiday calendar entry", async () => {
    const ctx = createToolContext({
      holidayCalendarEntry: {
        findUnique: vi.fn().mockResolvedValue(null),
      },
    });

    const result = await executeTool(
      "delete_holiday_calendar_entry",
      JSON.stringify({ id: "entry_missing" }),
      ctx,
    );

    expect(JSON.parse(result.content)).toEqual({
      error: "Holiday calendar entry not found with the given criteria.",
    });
  });
});
