import { NextResponse } from "next/server";
import { zodToJsonSchema } from "zod-to-json-schema";
import { getAuthFromRequest } from "@open-mercato/shared/lib/auth/server";
import { createRequestContainer } from "@open-mercato/shared/lib/di/container";
import { getToolRegistry } from "../../lib/tool-registry.js";
import { loadAllModuleTools } from "../../lib/tool-loader.js";
import { hasRequiredFeatures } from "../../lib/auth.js";
const openApi = {
  tag: "AI Assistant",
  summary: "List AI tools",
  methods: {
    GET: { summary: "List available MCP tools filtered by user permissions" }
  }
};
const metadata = {
  GET: { requireAuth: true, requireFeatures: ["ai_assistant.view"] }
};
async function GET(req) {
  const auth = await getAuthFromRequest(req);
  if (!auth) {
    return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
  }
  try {
    const container = await createRequestContainer();
    const rbacService = container.resolve("rbacService");
    const acl = await rbacService.loadAcl(auth.sub, {
      tenantId: auth.tenantId,
      organizationId: auth.orgId
    });
    await loadAllModuleTools();
    const registry = getToolRegistry();
    const allTools = Array.from(registry.getTools().values());
    const accessibleTools = allTools.filter(
      (tool) => hasRequiredFeatures(tool.requiredFeatures, acl.features, acl.isSuperAdmin, rbacService)
    );
    const tools = accessibleTools.map((tool) => {
      const nameParts = tool.name.split(".");
      return {
        name: tool.name,
        description: tool.description,
        inputSchema: zodToJsonSchema(tool.inputSchema),
        module: nameParts[0] || "other"
      };
    });
    return NextResponse.json({ tools });
  } catch (error) {
    console.error("[AI Tools] Error listing tools:", error);
    return NextResponse.json({ error: "Failed to list tools" }, { status: 500 });
  }
}
export {
  GET,
  metadata,
  openApi
};
//# sourceMappingURL=route.js.map
