import { createTRPCRouter, controllerProcedure, managerProcedure } from "../trpc.js";
import {
  CalculationRuleIdInputSchema,
  CreateCalculationRuleInputSchema,
  createCalculationRule,
  deleteCalculationRule,
  getCalculationRuleById,
  listActiveCalculationRules,
  listCalculationRules,
  UpdateCalculationRuleInputSchema,
  updateCalculationRule,
} from "./calculation-rule-procedure-support.js";

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

  getById: controllerProcedure
    .input(CalculationRuleIdInputSchema)
    .query(({ ctx, input }) => getCalculationRuleById(ctx, input)),

  /** Get all active rules (optimized for engine use — no project include) */
  getActive: controllerProcedure.query(({ ctx }) => listActiveCalculationRules(ctx)),

  create: managerProcedure
    .input(CreateCalculationRuleInputSchema)
    .mutation(({ ctx, input }) => createCalculationRule(ctx, input)),

  update: managerProcedure
    .input(UpdateCalculationRuleInputSchema)
    .mutation(({ ctx, input }) => updateCalculationRule(ctx, input)),

  delete: managerProcedure
    .input(CalculationRuleIdInputSchema)
    .mutation(({ ctx, input }) => deleteCalculationRule(ctx, input)),
});
