import { describe, expect, it } from "vitest";
import {
  buildTimelineEntriesDetailResponse,
  buildTimelineEntriesViewResponse,
} from "../router/timeline-entry-response-support.js";

describe("timeline entry response support", () => {
  const directory = {
    config: { enabled: true, mode: "alias", domain: "example.test" },
    byResourceId: new Map([
      [
        "resource_1",
        {
          displayName: "Anon Alice",
          eid: "ANON-001",
          email: "anon-alice@example.test",
        },
      ],
    ]),
    byAliasEid: new Map([["anon-001", "resource_1"]]),
  };

  it("builds anonymized entries view responses", () => {
    const readModel = {
      allocations: [
        {
          id: "allocation_1",
          projectId: "project_1",
          resourceId: "resource_1",
          resource: { id: "resource_1", displayName: "Alice", eid: "E-001" },
        },
      ],
      demands: [{ id: "demand_1", projectId: "project_1" }],
      assignments: [
        {
          id: "assignment_1",
          projectId: "project_1",
          resourceId: "resource_1",
          resource: { id: "resource_1", displayName: "Alice", eid: "E-001" },
        },
      ],
    };

    expect(buildTimelineEntriesViewResponse(readModel, directory as never)).toEqual({
      allocations: [
        {
          id: "allocation_1",
          projectId: "project_1",
          resourceId: "resource_1",
          resource: { id: "resource_1", displayName: "Anon Alice", eid: "ANON-001" },
        },
      ],
      demands: [{ id: "demand_1", projectId: "project_1" }],
      assignments: [
        {
          id: "assignment_1",
          projectId: "project_1",
          resourceId: "resource_1",
          resource: { id: "resource_1", displayName: "Anon Alice", eid: "ANON-001" },
        },
      ],
    });
  });

  it("builds detail responses with combined summary and anonymized entries", () => {
    const readModel = {
      allocations: [
        {
          id: "allocation_1",
          projectId: "project_1",
          resourceId: "resource_1",
          resource: { id: "resource_1", displayName: "Alice", eid: "E-001" },
        },
      ],
      demands: [{ id: "demand_1", projectId: "project_1" }],
      assignments: [
        {
          id: "assignment_1",
          projectId: "project_1",
          resourceId: "resource_1",
          resource: { id: "resource_1", displayName: "Alice", eid: "E-001" },
        },
      ],
    };
    const overlays = [{ id: "overlay_1", resourceId: "resource_1" }];

    expect(
      buildTimelineEntriesDetailResponse({
        period: {
          startDate: new Date("2026-04-01T00:00:00.000Z"),
          endDate: new Date("2026-04-05T00:00:00.000Z"),
        },
        filters: {
          resourceIds: ["resource_1"],
          projectIds: undefined,
          clientIds: undefined,
          chapters: undefined,
          eids: undefined,
          countryCodes: undefined,
        },
        readModel,
        directory: directory as never,
        holidayOverlays: overlays,
        holidaySummary: {
          overlayCount: 1,
          holidayResourceCount: 1,
          byScope: [{ scope: "COUNTRY", count: 1 }],
        },
      }),
    ).toEqual({
      period: {
        startDate: "2026-04-01",
        endDate: "2026-04-05",
      },
      filters: {
        resourceIds: ["resource_1"],
        projectIds: undefined,
        clientIds: undefined,
        chapters: undefined,
        eids: undefined,
        countryCodes: undefined,
      },
      summary: {
        allocationCount: 1,
        demandCount: 1,
        assignmentCount: 1,
        projectCount: 1,
        resourceCount: 1,
        overlayCount: 1,
        holidayResourceCount: 1,
        byScope: [{ scope: "COUNTRY", count: 1 }],
      },
      allocations: [
        {
          id: "allocation_1",
          projectId: "project_1",
          resourceId: "resource_1",
          resource: { id: "resource_1", displayName: "Anon Alice", eid: "ANON-001" },
        },
      ],
      demands: [{ id: "demand_1", projectId: "project_1" }],
      assignments: [
        {
          id: "assignment_1",
          projectId: "project_1",
          resourceId: "resource_1",
          resource: { id: "resource_1", displayName: "Anon Alice", eid: "ANON-001" },
        },
      ],
      holidayOverlays: overlays,
    });
  });
});
