import type { PrismaClient } from "@capakraken/db";
import { emitProjectShifted } from "../sse/event-bus.js";
import { loadProjectShiftContext } from "./timeline-project-load-support.js";
import { applyTimelineProjectShift } from "./timeline-shift-procedure-support.js";

export async function applyTimelineProjectShiftMutation(input: {
  db: PrismaClient;
  projectId: string;
  newStartDate: Date;
  newEndDate: Date;
}) {
  const context = await loadProjectShiftContext(input.db, input.projectId);
  const result = await applyTimelineProjectShift({
    db: input.db,
    projectId: input.projectId,
    newStartDate: input.newStartDate,
    newEndDate: input.newEndDate,
    context,
  });

  emitProjectShifted(result.event);

  return {
    project: result.project,
    validation: result.validation,
  };
}
