import { describe, expect, it } from "vitest";
import { buildResourceCapacitySeries } from "./timelineCapacity.js";
import type { TimelineAssignmentEntry, VacationEntry } from "./TimelineContext.js";

function createAssignmentWithAvailability(availability: Record<string, number>): TimelineAssignmentEntry {
  return {
    resource: {
      id: "resource-1",
      displayName: "Peter Parker",
      eid: "E-001",
      chapter: "Delivery",
      lcrCents: 10000,
      availability,
    },
  } as TimelineAssignmentEntry;
}

function buildSeries(args: { dates: Date[]; vacations?: VacationEntry[]; availability: Record<string, number> }) {
  return buildResourceCapacitySeries(
    new Map([["resource-1", [createAssignmentWithAvailability(args.availability)]]]),
    new Map([["resource-1", args.vacations ?? []]]),
    args.dates,
  ).get("resource-1");
}

describe("timelineCapacity", () => {
  it("maps local weekdays without UTC drift", () => {
    const series = buildSeries({
      dates: [
        new Date(2026, 2, 30),
        new Date(2026, 3, 4),
        new Date(2026, 3, 5),
      ],
      availability: {
        sunday: 1,
        monday: 6,
        tuesday: 7,
        wednesday: 8,
        thursday: 5,
        friday: 4,
        saturday: 2,
      },
    });

    expect(series?.baseHoursByDay).toEqual([6, 2, 1]);
    expect(series?.capacityHoursByDay).toEqual([6, 2, 1]);
    expect(series?.bookingFactorsByDay).toEqual([1, 1, 1]);
  });

  it("applies approved absences using the local calendar day key", () => {
    const series = buildSeries({
      dates: [new Date(2026, 2, 30)],
      availability: {
        sunday: 0,
        monday: 8,
        tuesday: 8,
        wednesday: 8,
        thursday: 8,
        friday: 8,
        saturday: 0,
      },
      vacations: [
        {
          id: "vac-1",
          resourceId: "resource-1",
          type: "ANNUAL",
          status: "APPROVED",
          startDate: "2026-03-30",
          endDate: "2026-03-30",
          isHalfDay: false,
        } as VacationEntry,
      ],
    });

    expect(series?.baseHoursByDay).toEqual([8]);
    expect(series?.capacityHoursByDay).toEqual([0]);
    expect(series?.bookingFactorsByDay).toEqual([0]);
  });
});
