import { describe, expect, it } from "vitest";
import {
  buildTimelineHolidayOverlayDetailResponse,
  buildTimelineHolidayResourceIds,
  buildTimelineHolidayResourceWhere,
  formatTimelineHolidayOverlays,
  summarizeTimelineHolidayOverlays,
} from "../router/timeline-holiday-support.js";

describe("timeline holiday support", () => {
  it("builds resource filters only when holiday resource constraints are present", () => {
    expect(buildTimelineHolidayResourceWhere({})).toBeNull();
    expect(buildTimelineHolidayResourceWhere({ chapters: ["3D"] })).toEqual({
      chapter: { in: ["3D"] },
    });
    expect(
      buildTimelineHolidayResourceWhere({
        chapters: ["3D"],
        eids: ["E-001"],
        countryCodes: ["DE"],
      }),
    ).toEqual({
      AND: [
        { chapter: { in: ["3D"] } },
        { eid: { in: ["E-001"] } },
        { country: { code: { in: ["DE"] } } },
      ],
    });
  });

  it("merges assignment, requested, and matched resource ids without duplicates", () => {
    expect(
      buildTimelineHolidayResourceIds({
        assignmentResourceIds: ["resource_1", null, "resource_2"],
        requestedResourceIds: ["resource_2", "resource_3"],
        matchingFilterResourceIds: ["resource_3", "resource_4"],
      }),
    ).toEqual(["resource_1", "resource_2", "resource_3", "resource_4"]);
  });

  it("formats overlays and summarizes them by scope", () => {
    const overlays = formatTimelineHolidayOverlays([
      {
        id: "overlay_1",
        resourceId: "resource_1",
        startDate: new Date("2026-04-03T00:00:00.000Z"),
        endDate: new Date("2026-04-03T00:00:00.000Z"),
        scope: "COUNTRY",
        note: "Holiday",
      },
      {
        id: "overlay_2",
        resourceId: "resource_2",
        startDate: new Date("2026-04-04T00:00:00.000Z"),
        endDate: new Date("2026-04-04T00:00:00.000Z"),
        scope: "CITY",
        note: "Holiday",
      },
      {
        id: "overlay_3",
        resourceId: "resource_1",
        startDate: new Date("2026-04-05T00:00:00.000Z"),
        endDate: new Date("2026-04-05T00:00:00.000Z"),
        note: "Holiday",
      },
    ]);

    expect(overlays).toEqual([
      expect.objectContaining({ startDate: "2026-04-03", endDate: "2026-04-03", scope: "COUNTRY" }),
      expect.objectContaining({ startDate: "2026-04-04", endDate: "2026-04-04", scope: "CITY" }),
      expect.objectContaining({ startDate: "2026-04-05", endDate: "2026-04-05", scope: null }),
    ]);
    expect(summarizeTimelineHolidayOverlays(overlays)).toEqual({
      overlayCount: 3,
      holidayResourceCount: 2,
      byScope: [
        { scope: "CITY", count: 1 },
        { scope: "COUNTRY", count: 1 },
        { scope: "UNKNOWN", count: 1 },
      ],
    });
  });

  it("builds holiday detail responses from formatted overlays", () => {
    const overlays = formatTimelineHolidayOverlays([
      {
        id: "overlay_1",
        resourceId: "resource_1",
        startDate: new Date("2026-04-03T00:00:00.000Z"),
        endDate: new Date("2026-04-03T00:00:00.000Z"),
        scope: "COUNTRY",
        note: "Holiday",
      },
    ]);

    expect(
      buildTimelineHolidayOverlayDetailResponse({
        startDate: new Date("2026-04-01T00:00:00.000Z"),
        endDate: new Date("2026-04-07T00:00:00.000Z"),
        filters: { countryCodes: ["DE"] },
        overlays,
      }),
    ).toEqual({
      period: {
        startDate: "2026-04-01",
        endDate: "2026-04-07",
      },
      filters: { countryCodes: ["DE"] },
      summary: {
        overlayCount: 1,
        holidayResourceCount: 1,
        byScope: [{ scope: "COUNTRY", count: 1 }],
      },
      overlays,
    });
  });
});
