const express = require('express');
const { query } = require('express-validator');
const router = express.Router();
const adminStatisticsController = require('../controllers/adminStatisticsController');
const { authenticateAdmin } = require('../middleware/auth');
const { logAdminOperation } = require('../middleware/adminLogger');
const { requireRole } = require('../middleware/rbac');

/**
 * Admin Statistics Routes
 * Base path: /api/v1/admin/statistics
 * All routes require admin authentication
 */

/**
 * @route   GET /api/v1/admin/statistics
 * @desc    Get comprehensive statistics data
 * @access  Private (Admin)
 */
router.get(
  '/',
  authenticateAdmin,
  requireRole(['super_admin', 'admin', 'operator']),
  [
    query('startDate').optional().isISO8601().withMessage('Start date must be a valid ISO 8601 date'),
    query('endDate').optional().isISO8601().withMessage('End date must be a valid ISO 8601 date'),
    query('groupBy')
      .optional()
      .isIn(['hour', 'day', 'week', 'month', 'year'])
      .withMessage('Group by must be one of: hour, day, week, month, year'),
    query('serviceLimit').optional().isInt({ min: 1, max: 100 }).withMessage('Service limit must be between 1 and 100'),
  ],
  logAdminOperation,
  adminStatisticsController.getStatistics
);

/**
 * @route   GET /api/v1/admin/statistics/dashboard
 * @desc    Get dashboard metrics
 * @access  Private (Admin)
 */
router.get(
  '/dashboard',
  authenticateAdmin,
  requireRole(['super_admin', 'admin', 'operator']),
  [
    query('startDate').optional().isISO8601().withMessage('Start date must be a valid ISO 8601 date'),
    query('endDate').optional().isISO8601().withMessage('End date must be a valid ISO 8601 date'),
  ],
  logAdminOperation,
  adminStatisticsController.getDashboardMetrics
);

/**
 * @route   GET /api/v1/admin/statistics/user-growth
 * @desc    Get user growth trends
 * @access  Private (Admin)
 */
router.get(
  '/user-growth',
  authenticateAdmin,
  requireRole(['super_admin', 'admin', 'operator']),
  [
    query('startDate').optional().isISO8601().withMessage('Start date must be a valid ISO 8601 date'),
    query('endDate').optional().isISO8601().withMessage('End date must be a valid ISO 8601 date'),
    query('groupBy')
      .optional()
      .isIn(['hour', 'day', 'week', 'month', 'year'])
      .withMessage('Group by must be one of: hour, day, week, month, year'),
  ],
  logAdminOperation,
  adminStatisticsController.getUserGrowthTrends
);

/**
 * @route   GET /api/v1/admin/statistics/service-rankings
 * @desc    Get service popularity rankings
 * @access  Private (Admin)
 */
router.get(
  '/service-rankings',
  authenticateAdmin,
  requireRole(['super_admin', 'admin', 'operator']),
  [
    query('startDate').optional().isISO8601().withMessage('Start date must be a valid ISO 8601 date'),
    query('endDate').optional().isISO8601().withMessage('End date must be a valid ISO 8601 date'),
    query('limit').optional().isInt({ min: 1, max: 100 }).withMessage('Limit must be between 1 and 100'),
  ],
  logAdminOperation,
  adminStatisticsController.getServicePopularityRankings
);

/**
 * @route   GET /api/v1/admin/statistics/invitation-conversion
 * @desc    Get invitation conversion rate
 * @access  Private (Admin)
 */
router.get(
  '/invitation-conversion',
  authenticateAdmin,
  requireRole(['super_admin', 'admin', 'operator']),
  [
    query('startDate').optional().isISO8601().withMessage('Start date must be a valid ISO 8601 date'),
    query('endDate').optional().isISO8601().withMessage('End date must be a valid ISO 8601 date'),
  ],
  logAdminOperation,
  adminStatisticsController.getInvitationConversionRate
);

/**
 * @route   GET /api/v1/admin/statistics/financial-report
 * @desc    Get financial report
 * @access  Private (Admin)
 */
router.get(
  '/financial-report',
  authenticateAdmin,
  requireRole(['super_admin', 'admin', 'operator']),
  [
    query('startDate').optional().isISO8601().withMessage('Start date must be a valid ISO 8601 date'),
    query('endDate').optional().isISO8601().withMessage('End date must be a valid ISO 8601 date'),
  ],
  logAdminOperation,
  adminStatisticsController.getFinancialReport
);

module.exports = router;
