import { beforeEach, describe, expect, it, vi } from "vitest";

import {
  createHolidayCalendar,
  createToolContext,
  executeTool,
} from "./assistant-tools-holiday-read-test-helpers.js";

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

  it("gets a holiday calendar by identifier and exposes ordered entries", async () => {
    const findUnique = vi.fn().mockResolvedValue(null);
    const findFirst = vi.fn().mockResolvedValue(createHolidayCalendar());
    const ctx = createToolContext({
      holidayCalendar: {
        findUnique,
        findFirst,
      },
    });

    const result = await executeTool(
      "get_holiday_calendar",
      JSON.stringify({ identifier: "Germany National" }),
      ctx,
    );

    const parsed = JSON.parse(result.content) as {
      id: string;
      name: string;
      entries: Array<{ name: string; date: string }>;
    };

    expect(findUnique).toHaveBeenCalledWith({
      where: { id: "Germany National" },
      include: {
        country: { select: { id: true, code: true, name: true } },
        metroCity: { select: { id: true, name: true } },
        entries: { orderBy: [{ date: "asc" }, { name: "asc" }] },
      },
    });
    expect(findFirst).toHaveBeenCalledWith({
      where: { name: { equals: "Germany National", mode: "insensitive" } },
      include: {
        country: { select: { id: true, code: true, name: true } },
        metroCity: { select: { id: true, name: true } },
        entries: { orderBy: [{ date: "asc" }, { name: "asc" }] },
      },
    });
    expect(parsed).toMatchObject({
      id: "cal_de",
      name: "Germany National",
      entries: [{ name: "New Year", date: "2026-01-01" }],
    });
  });

  it("resolves a calendar by fallback name lookup", async () => {
    const db = {
      holidayCalendar: {
        findUnique: vi.fn().mockResolvedValue(null),
        findFirst: vi.fn().mockResolvedValue(createHolidayCalendar()),
      },
    };
    const ctx = createToolContext(db);

    const result = await executeTool(
      "get_holiday_calendar",
      JSON.stringify({ identifier: "Germany National" }),
      ctx,
    );

    expect(db.holidayCalendar.findUnique).toHaveBeenCalledWith({
      where: { id: "Germany National" },
      include: {
        country: { select: { id: true, code: true, name: true } },
        metroCity: { select: { id: true, name: true } },
        entries: { orderBy: [{ date: "asc" }, { name: "asc" }] },
      },
    });
    expect(db.holidayCalendar.findFirst).toHaveBeenCalledWith({
      where: { name: { equals: "Germany National", mode: "insensitive" } },
      include: {
        country: { select: { id: true, code: true, name: true } },
        metroCity: { select: { id: true, name: true } },
        entries: { orderBy: [{ date: "asc" }, { name: "asc" }] },
      },
    });
    expect(JSON.parse(result.content)).toEqual(
      expect.objectContaining({
        id: "cal_de",
        name: "Germany National",
      }),
    );
  });
});
