import {
  ApplyEffortRulesSchema,
  CreateEffortRuleSetSchema,
  UpdateEffortRuleSetSchema,
} from "@capakraken/shared";
import { createTRPCRouter, controllerProcedure, managerProcedure } from "../trpc.js";
import {
  applyEffortRules,
  createEffortRuleSet,
  deleteEffortRuleSet,
  effortRuleIdInputSchema,
  effortRulePreviewInputSchema,
  getEffortRuleSetById,
  listEffortRuleSets,
  previewEffortRules,
  updateEffortRuleSet,
} from "./effort-rule-procedure-support.js";

export const effortRuleRouter = createTRPCRouter({
  list: controllerProcedure.query(({ ctx }) => listEffortRuleSets(ctx)),

  getById: controllerProcedure
    .input(effortRuleIdInputSchema)
    .query(({ ctx, input }) => getEffortRuleSetById(ctx, input)),

  create: managerProcedure
    .input(CreateEffortRuleSetSchema)
    .mutation(({ ctx, input }) => createEffortRuleSet(ctx, input)),

  update: managerProcedure
    .input(UpdateEffortRuleSetSchema)
    .mutation(({ ctx, input }) => updateEffortRuleSet(ctx, input)),

  delete: managerProcedure
    .input(effortRuleIdInputSchema)
    .mutation(({ ctx, input }) => deleteEffortRuleSet(ctx, input)),

  /** Preview the expansion result without persisting */
  preview: controllerProcedure
    .input(effortRulePreviewInputSchema)
    .query(({ ctx, input }) => previewEffortRules(ctx, input)),

  /** Apply effort rules to generate demand lines on the working version */
  applyRules: managerProcedure
    .input(ApplyEffortRulesSchema)
    .mutation(({ ctx, input }) => applyEffortRules(ctx, input)),
});
