import {
  adminProcedure,
  createTRPCRouter,
  protectedProcedure,
  resourceOverviewProcedure,
} from "../trpc.js";
import {
  createOrgUnit,
  deactivateOrgUnit,
  getOrgUnitById,
  getOrgUnitByIdentifier,
  getOrgUnitTree,
  listOrgUnits,
  orgUnitIdInputSchema,
  orgUnitIdentifierInputSchema,
  orgUnitListInputSchema,
  orgUnitTreeInputSchema,
  orgUnitUpdateInputSchema,
  resolveOrgUnitByIdentifier,
  updateOrgUnit,
} from "./org-unit-procedure-support.js";
import { CreateOrgUnitSchema } from "@capakraken/shared";

export const orgUnitRouter = createTRPCRouter({
  list: resourceOverviewProcedure
    .input(orgUnitListInputSchema)
    .query(({ ctx, input }) => listOrgUnits(ctx, input)),

  getTree: resourceOverviewProcedure
    .input(orgUnitTreeInputSchema)
    .query(({ ctx, input }) => getOrgUnitTree(ctx, input)),

  getById: resourceOverviewProcedure
    .input(orgUnitIdInputSchema)
    .query(({ ctx, input }) => getOrgUnitById(ctx, input)),

  resolveByIdentifier: protectedProcedure
    .input(orgUnitIdentifierInputSchema)
    .query(({ ctx, input }) => resolveOrgUnitByIdentifier(ctx, input)),

  getByIdentifier: resourceOverviewProcedure
    .input(orgUnitIdentifierInputSchema)
    .query(({ ctx, input }) => getOrgUnitByIdentifier(ctx, input)),

  create: adminProcedure
    .input(CreateOrgUnitSchema)
    .mutation(({ ctx, input }) => createOrgUnit(ctx, input)),

  update: adminProcedure
    .input(orgUnitUpdateInputSchema)
    .mutation(({ ctx, input }) => updateOrgUnit(ctx, input)),

  deactivate: adminProcedure
    .input(orgUnitIdInputSchema)
    .mutation(({ ctx, input }) => deactivateOrgUnit(ctx, input)),
});
