import { describe, expect, it, vi } from "vitest";

vi.mock("../sse/event-bus.js", () => ({
  emitProjectShifted: vi.fn(),
}));

vi.mock("../router/timeline-project-load-support.js", () => ({
  loadProjectShiftContext: vi.fn(),
}));

vi.mock("../router/timeline-shift-procedure-support.js", () => ({
  applyTimelineProjectShift: vi.fn(),
}));

import { emitProjectShifted } from "../sse/event-bus.js";
import { loadProjectShiftContext } from "../router/timeline-project-load-support.js";
import { applyTimelineProjectShiftMutation } from "../router/timeline-shift-router-support.js";
import { applyTimelineProjectShift } from "../router/timeline-shift-procedure-support.js";

const emitProjectShiftedMock = vi.mocked(emitProjectShifted);
const loadProjectShiftContextMock = vi.mocked(loadProjectShiftContext);
const applyTimelineProjectShiftMock = vi.mocked(applyTimelineProjectShift);

describe("timeline shift router support", () => {
  it("loads context, applies the shift, emits the event, and returns the response payload", async () => {
    const db = {} as never;
    const context = { project: { id: "project_1" } } as never;
    const result = {
      project: { id: "project_1" },
      validation: { valid: true },
      event: { projectId: "project_1" },
    } as never;

    loadProjectShiftContextMock.mockResolvedValueOnce(context);
    applyTimelineProjectShiftMock.mockResolvedValueOnce(result);

    await expect(
      applyTimelineProjectShiftMutation({
        db,
        projectId: "project_1",
        newStartDate: new Date("2026-04-03T00:00:00.000Z"),
        newEndDate: new Date("2026-04-12T00:00:00.000Z"),
      }),
    ).resolves.toEqual({
      project: { id: "project_1" },
      validation: { valid: true },
    });

    expect(loadProjectShiftContextMock).toHaveBeenCalledWith(db, "project_1");
    expect(applyTimelineProjectShiftMock).toHaveBeenCalledWith({
      db,
      projectId: "project_1",
      newStartDate: new Date("2026-04-03T00:00:00.000Z"),
      newEndDate: new Date("2026-04-12T00:00:00.000Z"),
      context,
    });
    expect(emitProjectShiftedMock).toHaveBeenCalledWith({ projectId: "project_1" });
  });
});
