import type { PrismaClient } from "@capakraken/db";
import {
  emitAllocationCreated,
  emitAllocationDeleted,
  emitAllocationUpdated,
} from "../sse/event-bus.js";
import {
  createTimelineBatchQuickAssignments,
  createTimelineQuickAssignment,
} from "./timeline-allocation-assignment-procedure-support.js";
import {
  carveTimelineAllocationRange,
  extractTimelineAllocationFragment,
} from "./timeline-allocation-fragment-support.js";
import { applyTimelineInlineAllocationUpdate } from "./timeline-allocation-inline-support.js";
import { shiftTimelineAllocations } from "./timeline-allocation-procedure-support.js";

export async function updateTimelineAllocationInlineMutation(input: {
  db: PrismaClient;
  allocationId: string;
  hoursPerDay?: number | undefined;
  startDate?: Date | null | undefined;
  endDate?: Date | null | undefined;
  includeSaturday?: boolean | undefined;
  role?: string | null | undefined;
}) {
  const updated = await applyTimelineInlineAllocationUpdate({
    db: input.db,
    allocationId: input.allocationId,
    hoursPerDay: input.hoursPerDay,
    startDate: input.startDate ?? undefined,
    endDate: input.endDate ?? undefined,
    includeSaturday: input.includeSaturday,
    role: input.role ?? undefined,
  });

  emitAllocationUpdated({
    id: updated.id,
    projectId: updated.projectId,
    resourceId: updated.resourceId,
  });

  return updated;
}

export async function createTimelineQuickAssignMutation(
  db: PrismaClient,
  input: Omit<Parameters<typeof createTimelineQuickAssignment>[1], "source">,
) {
  return createTimelineQuickAssignment(db, {
    ...input,
    source: "quickAssign",
  });
}

export async function createTimelineBatchQuickAssignMutation(
  db: PrismaClient,
  input: {
    assignments: Array<
      Omit<Parameters<typeof createTimelineBatchQuickAssignments>[1]["assignments"][number], "source">
    >;
  },
) {
  return createTimelineBatchQuickAssignments(db, {
    assignments: input.assignments.map((assignment) => ({
      ...assignment,
      source: "batchQuickAssign" as const,
    })),
  });
}

export async function applyTimelineAllocationBatchShiftMutation(
  db: PrismaClient,
  input: Parameters<typeof shiftTimelineAllocations>[1],
) {
  return shiftTimelineAllocations(db, input);
}

export async function carveTimelineAllocationRangeMutation(input: {
  db: PrismaClient;
  allocationId: string;
  startDate: Date;
  endDate: Date;
}) {
  const result = await carveTimelineAllocationRange({
    db: input.db,
    allocationId: input.allocationId,
    startDate: input.startDate,
    endDate: input.endDate,
  });

  for (const allocationId of result.updatedAllocationIds) {
    emitAllocationUpdated({
      id: allocationId,
      projectId: result.projectId,
      resourceId: result.resourceId,
    });
  }
  for (const allocationId of result.createdAllocationIds) {
    emitAllocationCreated({
      id: allocationId,
      projectId: result.projectId,
      resourceId: result.resourceId,
    });
  }
  for (const allocationId of result.deletedAllocationIds) {
    emitAllocationDeleted(allocationId, result.projectId, result.resourceId);
  }

  return result;
}

export async function extractTimelineAllocationFragmentMutation(input: {
  db: PrismaClient;
  allocationId: string;
  startDate: Date;
  endDate: Date;
}) {
  const result = await extractTimelineAllocationFragment({
    db: input.db,
    allocationId: input.allocationId,
    startDate: input.startDate,
    endDate: input.endDate,
  });

  for (const allocationId of result.updatedAllocationIds) {
    emitAllocationUpdated({
      id: allocationId,
      projectId: result.projectId,
      resourceId: result.resourceId,
    });
  }
  for (const allocationId of result.createdAllocationIds) {
    emitAllocationCreated({
      id: allocationId,
      projectId: result.projectId,
      resourceId: result.resourceId,
    });
  }

  return result;
}
