import { describe, expect, it } from "vitest";
import { buildAllocationBlockClickInfo, buildAllocationMutationPlan } from "./timelineAllocationActions.js";

const baseAlloc = {
  allocationId: "alloc-1",
  mutationAllocationId: null,
  projectId: "project-1",
  projectName: "Alpha",
  scope: "allocation" as const,
  allocationStartDate: new Date("2025-01-01T00:00:00.000Z"),
  allocationEndDate: new Date("2025-01-05T00:00:00.000Z"),
  originalStartDate: new Date("2025-01-01T00:00:00.000Z"),
  originalEndDate: new Date("2025-01-05T00:00:00.000Z"),
  currentStartDate: new Date("2025-01-02T00:00:00.000Z"),
  currentEndDate: new Date("2025-01-06T00:00:00.000Z"),
};

describe("timelineAllocationActions", () => {
  it("returns null click info when the drag lacks an allocation id or original dates", () => {
    expect(
      buildAllocationBlockClickInfo({
        allocationId: null,
        projectId: "project-1",
        projectName: "Alpha",
        originalStartDate: baseAlloc.originalStartDate,
        originalEndDate: baseAlloc.originalEndDate,
      }),
    ).toBeNull();

    expect(
      buildAllocationBlockClickInfo({
        allocationId: "alloc-1",
        projectId: "project-1",
        projectName: "Alpha",
        originalStartDate: null,
        originalEndDate: baseAlloc.originalEndDate,
      }),
    ).toBeNull();
  });

  it("builds click info with empty-string fallbacks for nullable project metadata", () => {
    expect(
      buildAllocationBlockClickInfo({
        allocationId: "alloc-1",
        projectId: null,
        projectName: null,
        originalStartDate: baseAlloc.originalStartDate,
        originalEndDate: baseAlloc.originalEndDate,
      }),
    ).toEqual({
      allocationId: "alloc-1",
      projectId: "",
      projectName: "",
      startDate: new Date("2025-01-01T00:00:00.000Z"),
      endDate: new Date("2025-01-05T00:00:00.000Z"),
    });
  });

  it("returns no mutation plan when the drag never produced complete current dates", () => {
    expect(
      buildAllocationMutationPlan({
        ...baseAlloc,
        allocationId: null,
      }),
    ).toBeNull();

    expect(
      buildAllocationMutationPlan({
        ...baseAlloc,
        currentEndDate: null,
      }),
    ).toBeNull();
  });

  it("builds mutation plans with fallback mutation ids and extraction flags", () => {
    expect(
      buildAllocationMutationPlan({
        ...baseAlloc,
        scope: "segment",
        allocationStartDate: new Date("2024-12-31T00:00:00.000Z"),
        projectName: null,
      }),
    ).toEqual({
      activeAllocationId: "alloc-1",
      currentStartDate: new Date("2025-01-02T00:00:00.000Z"),
      currentEndDate: new Date("2025-01-06T00:00:00.000Z"),
      baseMutationAllocationId: "alloc-1",
      requiresExtraction: true,
      pendingSnapshot: {
        allocationId: "alloc-1",
        mutationAllocationId: "alloc-1",
        projectName: "",
        before: {
          startDate: new Date("2025-01-01T00:00:00.000Z"),
          endDate: new Date("2025-01-05T00:00:00.000Z"),
        },
        after: {
          startDate: new Date("2025-01-02T00:00:00.000Z"),
          endDate: new Date("2025-01-06T00:00:00.000Z"),
        },
      },
    });
  });
});
