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

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

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

  it("lists holiday calendars with scope metadata and entry counts", async () => {
    const findMany = vi.fn().mockResolvedValue([
      createHolidayCalendar({
        id: "cal_by",
        name: "Bayern Feiertage",
        scopeType: "STATE",
        stateCode: "BY",
        priority: 10,
        country: { id: "country_de", code: "DE", name: "Deutschland" },
        _count: { entries: 2 },
        entries: [
          {
            id: "entry_1",
            date: new Date("2026-01-06T00:00:00.000Z"),
            name: "Heilige Drei Koenige",
            isRecurringAnnual: true,
            source: "state",
          },
        ],
      }),
    ]);
    const ctx = createToolContext({
      holidayCalendar: {
        findMany,
      },
    });

    const result = await executeTool(
      "list_holiday_calendars",
      JSON.stringify({ countryCode: "DE", scopeType: "STATE", includeInactive: true }),
      ctx,
    );

    const parsed = JSON.parse(result.content) as {
      count: number;
      calendars: Array<{
        name: string;
        scopeType: string;
        stateCode: string | null;
        entryCount: number;
        country: { code: string };
      }>;
    };

    expect(parsed.count).toBe(1);
    expect(parsed.calendars).toHaveLength(1);
    expect(findMany).toHaveBeenCalledWith({
      where: {
        country: { code: { equals: "DE", mode: "insensitive" } },
        scopeType: "STATE",
      },
      include: {
        country: { select: { id: true, code: true, name: true } },
        metroCity: { select: { id: true, name: true } },
        _count: { select: { entries: true } },
        entries: { orderBy: [{ date: "asc" }, { name: "asc" }] },
      },
      orderBy: [
        { country: { name: "asc" } },
        { scopeType: "asc" },
        { priority: "desc" },
        { name: "asc" },
      ],
    });
    expect(parsed.calendars[0]).toMatchObject({
      name: "Bayern Feiertage",
      scopeType: "STATE",
      stateCode: "BY",
      entryCount: 2,
      country: { code: "DE" },
    });
  });

  it("lists active holiday calendars by default", async () => {
    const db = {
      holidayCalendar: {
        findMany: vi.fn().mockResolvedValue([
          {
            ...createHolidayCalendar(),
            _count: { entries: 1 },
          },
        ]),
      },
    };
    const ctx = createToolContext(db);

    const result = await executeTool(
      "list_holiday_calendars",
      JSON.stringify({ countryCode: "de", scopeType: "COUNTRY" }),
      ctx,
    );

    expect(db.holidayCalendar.findMany).toHaveBeenCalledWith({
      where: {
        isActive: true,
        country: { code: { equals: "DE", mode: "insensitive" } },
        scopeType: "COUNTRY",
      },
      include: {
        country: { select: { id: true, code: true, name: true } },
        metroCity: { select: { id: true, name: true } },
        _count: { select: { entries: true } },
        entries: { orderBy: [{ date: "asc" }, { name: "asc" }] },
      },
      orderBy: [
        { country: { name: "asc" } },
        { scopeType: "asc" },
        { priority: "desc" },
        { name: "asc" },
      ],
    });
    expect(JSON.parse(result.content)).toEqual({
      count: 1,
      calendars: [
        expect.objectContaining({
          id: "cal_de",
          name: "Germany National",
          entryCount: 1,
        }),
      ],
    });
  });
});
