import { z } from "zod";
import { controllerProcedure, createTRPCRouter } from "../trpc.js";
import { listReportBlueprints, ReportBlueprintCatalogSchema } from "./report-blueprints-support.js";
import { reportQueryProcedures } from "./report-query-engine.js";
import { reportEntitySchema } from "./report-query-config.js";
import {
  DeleteReportTemplateInputSchema,
  deleteReportTemplate,
  listReportTemplates,
  SaveReportTemplateInputSchema,
  saveReportTemplate,
} from "./report-template-procedure-support.js";

export const reportRouter = createTRPCRouter({
  ...reportQueryProcedures,

  listBlueprints: controllerProcedure
    .input(z.object({ entity: reportEntitySchema.optional() }).default({}))
    .output(ReportBlueprintCatalogSchema)
    .query(({ input }) => listReportBlueprints(input.entity)),

  listTemplates: controllerProcedure.query(({ ctx }) => listReportTemplates(ctx)),

  saveTemplate: controllerProcedure
    .input(SaveReportTemplateInputSchema)
    .mutation(({ ctx, input }) => saveReportTemplate(ctx, input)),

  deleteTemplate: controllerProcedure
    .input(DeleteReportTemplateInputSchema)
    .mutation(({ ctx, input }) => deleteReportTemplate(ctx, input)),
});
