import {
  adminProcedure,
  createTRPCRouter,
  planningReadProcedure,
  protectedProcedure,
} from "../trpc.js";
import {
  batchDeleteBlueprints,
  blueprintBatchDeleteInputSchema,
  blueprintGlobalFieldDefsInputSchema,
  blueprintIdentifierInputSchema,
  blueprintIdInputSchema,
  blueprintListInputSchema,
  blueprintRolePresetsInputSchema,
  blueprintSetGlobalInputSchema,
  blueprintUpdateInputSchema,
  createBlueprint,
  deleteBlueprint,
  getBlueprintById,
  getBlueprintDetailByIdentifier,
  getGlobalBlueprintFieldDefs,
  listBlueprintSummaries,
  listBlueprints,
  resolveBlueprintByIdentifier,
  setBlueprintGlobal,
  updateBlueprint,
  updateBlueprintRolePresets,
} from "./blueprint-procedure-support.js";
import { CreateBlueprintSchema } from "@capakraken/shared";

export const blueprintRouter = createTRPCRouter({
  listSummaries: planningReadProcedure.query(({ ctx }) => listBlueprintSummaries(ctx)),

  list: planningReadProcedure
    .input(blueprintListInputSchema)
    .query(({ ctx, input }) => listBlueprints(ctx, input)),

  getById: planningReadProcedure
    .input(blueprintIdInputSchema)
    .query(({ ctx, input }) => getBlueprintById(ctx, input)),

  resolveByIdentifier: protectedProcedure
    .input(blueprintIdentifierInputSchema)
    .query(({ ctx, input }) => resolveBlueprintByIdentifier(ctx, input)),

  getByIdentifier: planningReadProcedure
    .input(blueprintIdentifierInputSchema)
    .query(({ ctx, input }) => getBlueprintDetailByIdentifier(ctx, input)),

  create: adminProcedure
    .input(CreateBlueprintSchema)
    .mutation(({ ctx, input }) => createBlueprint(ctx, input)),

  update: adminProcedure
    .input(blueprintUpdateInputSchema)
    .mutation(({ ctx, input }) => updateBlueprint(ctx, input)),

  /** Dedicated mutation for saving role presets — separate from field defs to avoid Zod depth issues */
  updateRolePresets: adminProcedure
    .input(blueprintRolePresetsInputSchema)
    .mutation(({ ctx, input }) => updateBlueprintRolePresets(ctx, input)),

  delete: adminProcedure
    .input(blueprintIdInputSchema)
    .mutation(({ ctx, input }) => deleteBlueprint(ctx, input)),

  batchDelete: adminProcedure
    .input(blueprintBatchDeleteInputSchema)
    .mutation(({ ctx, input }) => batchDeleteBlueprints(ctx, input)),

  getGlobalFieldDefs: planningReadProcedure
    .input(blueprintGlobalFieldDefsInputSchema)
    .query(({ ctx, input }) => getGlobalBlueprintFieldDefs(ctx, input)),

  setGlobal: adminProcedure
    .input(blueprintSetGlobalInputSchema)
    .mutation(({ ctx, input }) => setBlueprintGlobal(ctx, input)),
});
