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

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

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

import { emitAllocationUpdated } from "../sse/event-bus.js";
import { shiftTimelineAllocations } from "../router/timeline-allocation-procedure-support.js";
import { applyTimelineBatchAllocationShift } from "../router/timeline-allocation-shift-support.js";

const emitAllocationUpdatedMock = vi.mocked(emitAllocationUpdated);
const applyTimelineBatchAllocationShiftMock = vi.mocked(applyTimelineBatchAllocationShift);

describe("timeline allocation procedure support", () => {
  beforeEach(() => {
    vi.clearAllMocks();
  });

  it("applies allocation shifts and emits update events", async () => {
    applyTimelineBatchAllocationShiftMock.mockResolvedValueOnce([
      {
        id: "allocation_1",
        projectId: "project_1",
        resourceId: "resource_1",
      },
      {
        id: "allocation_2",
        projectId: "project_2",
        resourceId: "resource_2",
      },
    ] as never);

    await expect(
      shiftTimelineAllocations({} as never, {
        allocationIds: ["allocation_1", "allocation_2"],
        daysDelta: 2,
        mode: "move",
      }),
    ).resolves.toEqual({ count: 2 });

    expect(applyTimelineBatchAllocationShiftMock).toHaveBeenCalledWith({
      db: {},
      allocationIds: ["allocation_1", "allocation_2"],
      daysDelta: 2,
      mode: "move",
    });
    expect(emitAllocationUpdatedMock).toHaveBeenNthCalledWith(1, {
      id: "allocation_1",
      projectId: "project_1",
      resourceId: "resource_1",
    });
    expect(emitAllocationUpdatedMock).toHaveBeenNthCalledWith(2, {
      id: "allocation_2",
      projectId: "project_2",
      resourceId: "resource_2",
    });
  });
});
