import type { PrismaClient } from "@capakraken/db";
import { PermissionKey } from "@capakraken/shared";
import { managerProcedure, requirePermission } from "../trpc.js";
import {
  UpdateAllocationHoursSchema,
  timelineCarveAllocationRangeInputSchema,
  timelineBatchQuickAssignInputSchema,
  timelineBatchShiftAllocationsInputSchema,
  timelineExtractAllocationFragmentInputSchema,
  timelineQuickAssignInputSchema,
} from "./timeline-allocation-mutation-schema-support.js";
import {
  applyTimelineAllocationBatchShiftMutation,
  carveTimelineAllocationRangeMutation,
  createTimelineBatchQuickAssignMutation,
  createTimelineQuickAssignMutation,
  extractTimelineAllocationFragmentMutation,
  updateTimelineAllocationInlineMutation,
} from "./timeline-allocation-router-support.js";

export const timelineAllocationMutationProcedures = {
  updateAllocationInline: managerProcedure
    .input(UpdateAllocationHoursSchema)
    .mutation(async ({ ctx, input }) => {
      requirePermission(ctx, PermissionKey.MANAGE_ALLOCATIONS);
      return updateTimelineAllocationInlineMutation({
        db: ctx.db as PrismaClient,
        allocationId: input.allocationId,
        hoursPerDay: input.hoursPerDay,
        startDate: input.startDate,
        endDate: input.endDate,
        includeSaturday: input.includeSaturday,
        role: input.role,
      });
    }),

  quickAssign: managerProcedure
    .input(timelineQuickAssignInputSchema)
    .mutation(async ({ ctx, input }) => {
      requirePermission(ctx, PermissionKey.MANAGE_ALLOCATIONS);
      return createTimelineQuickAssignMutation(ctx.db as PrismaClient, input);
    }),

  batchQuickAssign: managerProcedure
    .input(timelineBatchQuickAssignInputSchema)
    .mutation(async ({ ctx, input }) => {
      requirePermission(ctx, PermissionKey.MANAGE_ALLOCATIONS);
      return createTimelineBatchQuickAssignMutation(ctx.db as PrismaClient, input);
    }),

  batchShiftAllocations: managerProcedure
    .input(timelineBatchShiftAllocationsInputSchema)
    .mutation(async ({ ctx, input }) => {
      requirePermission(ctx, PermissionKey.MANAGE_ALLOCATIONS);
      return applyTimelineAllocationBatchShiftMutation(ctx.db as PrismaClient, input);
    }),

  carveAllocationRange: managerProcedure
    .input(timelineCarveAllocationRangeInputSchema)
    .mutation(async ({ ctx, input }) => {
      requirePermission(ctx, PermissionKey.MANAGE_ALLOCATIONS);
      return carveTimelineAllocationRangeMutation({
        db: ctx.db as PrismaClient,
        allocationId: input.allocationId,
        startDate: input.startDate,
        endDate: input.endDate,
      });
    }),

  extractAllocationFragment: managerProcedure
    .input(timelineExtractAllocationFragmentInputSchema)
    .mutation(async ({ ctx, input }) => {
      requirePermission(ctx, PermissionKey.MANAGE_ALLOCATIONS);
      return extractTimelineAllocationFragmentMutation({
        db: ctx.db as PrismaClient,
        allocationId: input.allocationId,
        startDate: input.startDate,
        endDate: input.endDate,
      });
    }),
};
