import { describe, expect, it } from "vitest";
import { resolveAllocationRelease } from "./timelineAllocationRelease.js";

const BASE_ALLOC = {
  isActive: true,
  mode: "move" as const,
  pointerDeltaX: 0,
  daysDelta: 0,
  allocationId: "alloc-1",
  mutationAllocationId: null,
  projectId: "project-1",
  projectName: "Project One",
  scope: "allocation" as const,
  allocationStartDate: new Date("2025-01-01"),
  allocationEndDate: new Date("2025-01-03"),
  originalStartDate: new Date("2025-01-01"),
  originalEndDate: new Date("2025-01-03"),
  currentStartDate: new Date("2025-01-01"),
  currentEndDate: new Date("2025-01-03"),
};

describe("timelineAllocationRelease", () => {
  it("ignores inactive drags", () => {
    expect(resolveAllocationRelease({ ...BASE_ALLOC, isActive: false }, { clickThresholdPx: 5, wasShift: false })).toEqual({
      kind: "ignore",
      preservePreview: false,
    });
  });

  it("routes shift-click releases to multi-select toggling", () => {
    expect(resolveAllocationRelease(BASE_ALLOC, { clickThresholdPx: 5, wasShift: true })).toEqual({
      kind: "shift-click",
      allocationId: "alloc-1",
      preservePreview: false,
    });
  });

  it("falls back to reset when click info cannot be built", () => {
    expect(
      resolveAllocationRelease(
        {
          ...BASE_ALLOC,
          originalStartDate: null,
          originalEndDate: null,
        },
        { clickThresholdPx: 5, wasShift: false },
      ),
    ).toEqual({
      kind: "reset",
      preservePreview: false,
    });
  });

  it("builds a mutation outcome when the allocation dates changed", () => {
    const currentStartDate = new Date("2025-01-02");
    const currentEndDate = new Date("2025-01-04");

    expect(
      resolveAllocationRelease(
        {
          ...BASE_ALLOC,
          daysDelta: 1,
          pointerDeltaX: 20,
          currentStartDate,
          currentEndDate,
        },
        { clickThresholdPx: 5, wasShift: false },
      ),
    ).toEqual({
      kind: "mutation",
      preservePreview: true,
      mutationPlan: {
        activeAllocationId: "alloc-1",
        currentStartDate,
        currentEndDate,
        baseMutationAllocationId: "alloc-1",
        requiresExtraction: false,
        pendingSnapshot: {
          allocationId: "alloc-1",
          mutationAllocationId: "alloc-1",
          projectName: "Project One",
          before: {
            startDate: new Date("2025-01-01"),
            endDate: new Date("2025-01-03"),
          },
          after: {
            startDate: currentStartDate,
            endDate: currentEndDate,
          },
        },
      },
    });
  });

  it("resets changed segment drags when no allocation id remains available", () => {
    expect(
      resolveAllocationRelease(
        {
          ...BASE_ALLOC,
          allocationId: null,
          scope: "segment",
          daysDelta: 1,
          pointerDeltaX: 20,
          currentStartDate: new Date("2025-01-02"),
          currentEndDate: new Date("2025-01-04"),
        },
        { clickThresholdPx: 5, wasShift: false },
      ),
    ).toEqual({
      kind: "reset",
      preservePreview: true,
    });
  });
});
