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

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

describe("assistant holiday resolution regional and resource tools", () => {
  beforeEach(() => {
    vi.clearAllMocks();
  });

  it("lists regional holidays and distinguishes Bavaria from Hamburg", async () => {
    const ctx = createToolContext({});

    const bavaria = await executeTool(
      "list_holidays_by_region",
      JSON.stringify({ countryCode: "DE", federalState: "BY", year: 2026 }),
      ctx,
    );
    const hamburg = await executeTool(
      "list_holidays_by_region",
      JSON.stringify({ countryCode: "DE", federalState: "HH", year: 2026 }),
      ctx,
    );

    const bavariaResult = JSON.parse(bavaria.content) as {
      count: number;
      locationContext: { federalState: string | null };
      summary: { byScope: Array<{ scope: string; count: number }> };
      holidays: Array<{ name: string; date: string }>;
    };
    const hamburgResult = JSON.parse(hamburg.content) as {
      count: number;
      locationContext: { federalState: string | null };
      holidays: Array<{ name: string; date: string }>;
    };

    expect(bavariaResult.count).toBeGreaterThan(hamburgResult.count);
    expect(bavariaResult.locationContext.federalState).toBe("BY");
    expect(bavariaResult.summary.byScope).toEqual(
      expect.arrayContaining([expect.objectContaining({ scope: "STATE" })]),
    );
    expect(bavariaResult.holidays).toEqual(
      expect.arrayContaining([
        expect.objectContaining({ name: "Heilige Drei Könige", date: "2026-01-06" }),
      ]),
    );
    expect(hamburgResult.holidays).not.toEqual(
      expect.arrayContaining([
        expect.objectContaining({ name: "Heilige Drei Könige", date: "2026-01-06" }),
      ]),
    );
  });

  it("resolves resource-specific holidays including city-local dates", async () => {
    const db = {
      resource: {
        findUnique: vi
          .fn()
          .mockResolvedValueOnce(null)
          .mockResolvedValueOnce({ id: "res_1", eid: "bruce.banner", displayName: "Bruce Banner" })
          .mockResolvedValueOnce({
            id: "res_1",
            eid: "bruce.banner",
            displayName: "Bruce Banner",
            federalState: "BY",
            countryId: "country_de",
            metroCityId: "city_augsburg",
            country: { code: "DE", name: "Deutschland" },
            metroCity: { name: "Augsburg" },
          }),
        findFirst: vi.fn(),
      },
    };
    const ctx = createToolContext(db);

    const result = await executeTool(
      "get_resource_holidays",
      JSON.stringify({ identifier: "bruce.banner", year: 2026 }),
      ctx,
    );

    const parsed = JSON.parse(result.content) as {
      resource: { eid: string; federalState: string | null; metroCity: string | null };
      summary: { byScope: Array<{ scope: string; count: number }> };
      holidays: Array<{ name: string; date: string }>;
    };

    expect(parsed.resource).toEqual(
      expect.objectContaining({
        eid: "bruce.banner",
        federalState: "BY",
        metroCity: "Augsburg",
      }),
    );
    expect(parsed.holidays).toEqual(
      expect.arrayContaining([
        expect.objectContaining({ name: "Augsburger Friedensfest", date: "2026-08-08" }),
      ]),
    );
    expect(parsed.summary.byScope).toEqual(
      expect.arrayContaining([expect.objectContaining({ scope: "CITY" })]),
    );
  });
});
