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

  it("creates, updates, and deletes holiday calendar entries for admin users", async () => {
    const holidayEntryCreate = vi.fn().mockResolvedValue({
      id: "entry_1",
      date: new Date("2026-01-01T00:00:00.000Z"),
      name: "New Year",
      isRecurringAnnual: true,
      source: "seed",
    });
    const holidayEntryUpdate = vi.fn().mockResolvedValue({
      id: "entry_1",
      date: new Date("2026-01-02T00:00:00.000Z"),
      name: "New Year Observed",
      isRecurringAnnual: false,
      source: null,
    });
    const holidayEntryDelete = vi.fn().mockResolvedValue({ id: "entry_1" });
    const ctx = createToolContext({
      holidayCalendar: {
        findUnique: vi.fn().mockResolvedValue({
          id: "cal_de",
          name: "Germany National Updated",
          scopeType: "COUNTRY",
          countryId: "country_de",
        }),
      },
      holidayCalendarEntry: {
        findFirst: vi.fn()
          .mockResolvedValueOnce(null)
          .mockResolvedValueOnce(null),
        findUnique: vi.fn()
          .mockResolvedValueOnce({
            id: "entry_1",
            name: "New Year",
            date: new Date("2026-01-01T00:00:00.000Z"),
            holidayCalendarId: "cal_de",
          })
          .mockResolvedValueOnce({
            id: "entry_1",
            name: "New Year Observed",
          }),
        create: holidayEntryCreate,
        update: holidayEntryUpdate,
        delete: holidayEntryDelete,
      },
    });

    const createEntryResult = await executeTool(
      "create_holiday_calendar_entry",
      JSON.stringify({
        holidayCalendarId: "cal_de",
        date: "2026-01-01",
        name: "New Year",
        isRecurringAnnual: true,
        source: "seed",
      }),
      ctx,
    );
    const updateEntryResult = await executeTool(
      "update_holiday_calendar_entry",
      JSON.stringify({
        id: "entry_1",
        data: { date: "2026-01-02", name: "New Year Observed", isRecurringAnnual: false, source: null },
      }),
      ctx,
    );
    const deleteEntryResult = await executeTool(
      "delete_holiday_calendar_entry",
      JSON.stringify({ id: "entry_1" }),
      ctx,
    );

    expect(holidayEntryCreate).toHaveBeenCalledWith({
      data: {
        holidayCalendarId: "cal_de",
        date: new Date("2026-01-01T00:00:00.000Z"),
        name: "New Year",
        isRecurringAnnual: true,
        source: "seed",
      },
    });
    expect(holidayEntryUpdate).toHaveBeenCalledWith({
      where: { id: "entry_1" },
      data: {
        date: new Date("2026-01-02T00:00:00.000Z"),
        name: "New Year Observed",
        isRecurringAnnual: false,
        source: null,
      },
    });
    expect(holidayEntryDelete).toHaveBeenCalledWith({ where: { id: "entry_1" } });
    expect(JSON.parse(createEntryResult.content)).toEqual(
      expect.objectContaining({ success: true, message: "Created holiday entry: New Year" }),
    );
    expect(JSON.parse(updateEntryResult.content)).toEqual(
      expect.objectContaining({ success: true, message: "Updated holiday entry: New Year Observed" }),
    );
    expect(JSON.parse(deleteEntryResult.content)).toEqual(
      expect.objectContaining({ success: true, message: "Deleted holiday entry: New Year Observed" }),
    );
  });
});
