/**
 * Analytics routes for SentryAgent.ai AgentIdP.
 * Exposes tenant analytics endpoints under /api/v1/analytics.
 * All routes require a valid Bearer JWT (authMiddleware).
 */

import { Router, RequestHandler } from 'express';
import { AnalyticsController } from '../controllers/AnalyticsController.js';
import { asyncHandler } from '../utils/asyncHandler.js';

/**
 * Creates and returns the Express router for analytics endpoints.
 *
 * Routes:
 *   GET /analytics/tokens           — daily token issuance trend (last N days)
 *   GET /analytics/agents/activity  — agent activity heatmap by dow+hour
 *   GET /analytics/agents           — per-agent usage summary for current month
 *
 * @param analyticsController - The analytics controller instance.
 * @param authMiddleware - The JWT authentication middleware for all protected endpoints.
 * @returns Configured Express router.
 */
export function createAnalyticsRouter(
  analyticsController: AnalyticsController,
  authMiddleware: RequestHandler,
): Router {
  const router = Router();

  // All analytics routes require authentication
  router.use(authMiddleware);

  // GET /analytics/tokens — daily token issuance trend
  router.get(
    '/tokens',
    asyncHandler(analyticsController.getTokenTrend.bind(analyticsController)),
  );

  // GET /analytics/agents/activity — agent activity heatmap (must be registered before /agents)
  router.get(
    '/agents/activity',
    asyncHandler(analyticsController.getAgentActivity.bind(analyticsController)),
  );

  // GET /analytics/agents — per-agent usage summary
  router.get(
    '/agents',
    asyncHandler(analyticsController.getAgentSummary.bind(analyticsController)),
  );

  return router;
}
