import { describe, expect, it } from "vitest";
import { buildProjectShiftMutationInput, createProjectDragState } from "./timelineProjectDrag.js";

type TestProjectDragState = {
  isDragging: boolean;
  projectId: string | null;
  projectName: string | null;
  allocationId: string | null;
  originalStartDate: Date | null;
  originalEndDate: Date | null;
  currentStartDate: Date | null;
  currentEndDate: Date | null;
  startMouseX: number;
  pointerDeltaX: number;
  originalLeft: number;
  blockWidth: number;
  daysDelta: number;
};

describe("timelineProjectDrag", () => {
  it("creates project-bar drag state with default legacy metrics cleared", () => {
    expect(
      createProjectDragState<TestProjectDragState>({
        projectId: "project-1",
        projectName: "Alpha",
        startDate: new Date("2025-01-10T00:00:00.000Z"),
        endDate: new Date("2025-01-20T00:00:00.000Z"),
        startMouseX: 240,
      }),
    ).toEqual({
      isDragging: true,
      projectId: "project-1",
      projectName: "Alpha",
      allocationId: null,
      originalStartDate: new Date("2025-01-10T00:00:00.000Z"),
      originalEndDate: new Date("2025-01-20T00:00:00.000Z"),
      currentStartDate: new Date("2025-01-10T00:00:00.000Z"),
      currentEndDate: new Date("2025-01-20T00:00:00.000Z"),
      startMouseX: 240,
      pointerDeltaX: 0,
      originalLeft: 0,
      blockWidth: 0,
      daysDelta: 0,
    });
  });

  it("preserves allocation metadata and block geometry for legacy allocation-block drags", () => {
    expect(
      createProjectDragState<TestProjectDragState>({
        projectId: "project-1",
        projectName: "Alpha",
        allocationId: "alloc-7",
        startDate: new Date("2025-01-10T00:00:00.000Z"),
        endDate: new Date("2025-01-20T00:00:00.000Z"),
        startMouseX: 320,
        originalLeft: 144,
        blockWidth: 96,
      }),
    ).toMatchObject({
      allocationId: "alloc-7",
      originalLeft: 144,
      blockWidth: 96,
    });
  });

  it("refuses to build mutation input for no-op drags or incomplete state", () => {
    expect(
      buildProjectShiftMutationInput({
        projectId: "project-1",
        currentStartDate: new Date("2025-01-10T00:00:00.000Z"),
        currentEndDate: new Date("2025-01-20T00:00:00.000Z"),
        daysDelta: 0,
      }),
    ).toBeNull();

    expect(
      buildProjectShiftMutationInput({
        projectId: null,
        currentStartDate: new Date("2025-01-10T00:00:00.000Z"),
        currentEndDate: new Date("2025-01-20T00:00:00.000Z"),
        daysDelta: 2,
      }),
    ).toBeNull();
  });

  it("builds mutation input only when a real drag produced complete dates", () => {
    expect(
      buildProjectShiftMutationInput({
        projectId: "project-1",
        currentStartDate: new Date("2025-01-12T00:00:00.000Z"),
        currentEndDate: new Date("2025-01-22T00:00:00.000Z"),
        daysDelta: 2,
      }),
    ).toEqual({
      projectId: "project-1",
      newStartDate: new Date("2025-01-12T00:00:00.000Z"),
      newEndDate: new Date("2025-01-22T00:00:00.000Z"),
    });
  });
});
