/**
 * Agent Registry routes for SentryAgent.ai AgentIdP.
 * Wires AgentController handlers to Express paths with auth, OPA, and rateLimit middleware.
 */

import { Router, RequestHandler } from 'express';
import { AgentController } from '../controllers/AgentController.js';
import { authMiddleware } from '../middleware/auth.js';
import { rateLimitMiddleware } from '../middleware/rateLimit.js';
import { asyncHandler } from '../utils/asyncHandler.js';

/**
 * Creates and returns the Express router for agent registry endpoints.
 *
 * @param agentController - The agent controller instance.
 * @param opaMiddleware - The OPA authorization middleware created at startup.
 * @returns Configured Express router.
 */
export function createAgentsRouter(agentController: AgentController, opaMiddleware: RequestHandler): Router {
  const router = Router();

  router.use(asyncHandler(authMiddleware));
  router.use(opaMiddleware);
  router.use(asyncHandler(rateLimitMiddleware));

  // POST /agents — Register a new agent
  router.post('/', asyncHandler(agentController.registerAgent.bind(agentController)));

  // GET /agents — List agents with optional filters
  router.get('/', asyncHandler(agentController.listAgents.bind(agentController)));

  // GET /agents/:agentId — Get a single agent
  router.get('/:agentId', asyncHandler(agentController.getAgentById.bind(agentController)));

  // PATCH /agents/:agentId — Update agent metadata
  router.patch('/:agentId', asyncHandler(agentController.updateAgent.bind(agentController)));

  // DELETE /agents/:agentId — Decommission an agent
  router.delete('/:agentId', asyncHandler(agentController.decommissionAgent.bind(agentController)));

  return router;
}
