import type { FastifyInstance } from "fastify";
import { canUseN8nWorkflow, loadN8nWorkflows, toPublicN8nWorkflow } from "./config.js";
import { getAuthUser } from "../auth/index.js";

export const registerN8nRoutes = async (app: FastifyInstance) => {
  app.get("/api/n8n-workflows", async (request) => {
    const user = await getAuthUser(request);
    const items = loadN8nWorkflows()
      .filter((wf) => canUseN8nWorkflow(user.roles, wf))
      .map(toPublicN8nWorkflow);
    return { items };
  });
};
