import { ApiResponse } from '@goodgo/types';
import { Router } from 'express';

import { authenticate } from '../middlewares/auth.middleware';
import { createFeatureRouter } from '../modules/feature/feature.module';
import { HealthController } from '../modules/health/health.controller';
import { MetricsController } from '../modules/metrics/metrics.controller';


export const createRouter = (): Router => {
  const router = Router();
  const healthController = new HealthController();

  const apiVersion = process.env.API_VERSION || 'v1';

  // EN: Health check endpoints
  // VI: Endpoints kiểm tra sức khỏe
  /**
   * @swagger
   * /health:
   *   get:
   *     summary: Basic liveness probe
   *     description: Returns basic health status for liveness probes
   *     tags: [Health]
   *     responses:
   *       200:
   *         description: Service is healthy
   *         content:
   *           application/json:
   *             schema:
   *               $ref: '#/components/schemas/HealthResponse'
   */
  router.get('/health', healthController.health);

  /**
   * @swagger
   * /health/ready:
   *   get:
   *     summary: Readiness probe
   *     description: Checks if service is ready to handle requests (includes database connectivity)
   *     tags: [Health]
   *     responses:
   *       200:
   *         description: Service is ready
   *         content:
   *           application/json:
   *             schema:
   *               $ref: '#/components/schemas/ReadinessResponse'
   *       503:
   *         description: Service is not ready
   *         content:
   *           application/json:
   *             schema:
   *               $ref: '#/components/schemas/ErrorResponse'
   */
  router.get('/health/ready', healthController.ready);

  /**
   * @swagger
   * /health/live:
   *   get:
   *     summary: Liveness probe
   *     description: Basic liveness check for container orchestration
   *     tags: [Health]
   *     responses:
   *       200:
   *         description: Service is alive
   *         content:
   *           application/json:
   *             schema:
   *               $ref: '#/components/schemas/LivenessResponse'
   */
  router.get('/health/live', healthController.live);

  // EN: Authentication demo endpoint
  // VI: Endpoint demo xác thực
  /**
   * @swagger
   * /auth/me:
   *   get:
   *     summary: Get current user information
   *     description: Returns information about the currently authenticated user
   *     tags: [Authentication]
   *     security:
   *       - bearerAuth: []
   *     responses:
   *       200:
   *         description: User information retrieved successfully
   *         content:
   *           application/json:
   *             schema:
   *               allOf:
   *                 - $ref: '#/components/schemas/ApiResponse'
   *                 - type: object
   *                   properties:
   *                     data:
   *                       $ref: '#/components/schemas/UserInfo'
   *       401:
   *         description: Authentication required
   *         content:
   *           application/json:
   *             schema:
   *               $ref: '#/components/schemas/ErrorResponse'
   */
  router.get('/auth/me', authenticate(), (req, res) => {
    const response: ApiResponse = {
      success: true,
      data: req.user,
      message: 'User information retrieved / Thông tin người dùng đã được lấy',
      timestamp: new Date().toISOString(),
    };
    res.json(response);
  });

  // API routes
  router.use(`/api/${apiVersion}/features`, createFeatureRouter());

  // EN: Metrics endpoint
  // VI: Endpoint metrics
  /**
   * @swagger
   * /metrics:
   *   get:
   *     summary: Get Prometheus metrics
   *     description: Returns application metrics in Prometheus format for monitoring
   *     tags: [Monitoring]
   *     responses:
   *       200:
   *         description: Metrics in Prometheus format
   *         content:
   *           text/plain:
   *             schema:
   *               type: string
   *               example: "# HELP http_requests_total Total number of HTTP requests\n# TYPE http_requests_total counter\nhttp_requests_total{method=\"GET\",route=\"/health\",status=\"200\"} 42"
   */
  const metricsController = new MetricsController();
  router.get('/metrics', metricsController.getMetrics);

  return router;
};

