const express = require('express');
const adminCommissionController = require('../controllers/adminCommissionController');
const { authenticateAdmin } = require('../middleware/auth');
const { requireRole } = require('../middleware/rbac');
const { logAdminOperation } = require('../middleware/adminLogger');

const router = express.Router();

/**
 * Admin Commission Routes
 */

/**
 * @route   GET /api/v1/admin/commissions
 * @desc    Get all commissions
 * @access  Private (Admin)
 */
router.get(
  '/',
  authenticateAdmin,
  requireRole(['super_admin', 'admin', 'operator']),
  adminCommissionController.getCommissions
);

/**
 * @route   GET /api/v1/admin/commissions/stats
 * @desc    Get commission statistics
 * @access  Private (Admin)
 */
router.get(
  '/stats',
  authenticateAdmin,
  requireRole(['super_admin', 'admin', 'operator']),
  adminCommissionController.getCommissionStats
);

/**
 * @route   GET /api/v1/admin/config/commission-rate
 * @desc    Get commission rate
 * @access  Private (Admin)
 */
router.get(
  '/config/commission-rate',
  authenticateAdmin,
  requireRole(['super_admin', 'admin']),
  adminCommissionController.getCommissionRate
);

/**
 * @route   PUT /api/v1/admin/config/commission-rate
 * @desc    Set commission rate
 * @access  Private (Super Admin only)
 */
router.put(
  '/config/commission-rate',
  authenticateAdmin,
  requireRole(['super_admin']),
  logAdminOperation,
  adminCommissionController.setCommissionRate
);

/**
 * @route   GET /api/v1/admin/config/user-commission-rates
 * @desc    Get all users with custom commission rates
 * @access  Private (Admin)
 */
router.get(
  '/config/user-commission-rates',
  authenticateAdmin,
  requireRole(['super_admin', 'admin']),
  adminCommissionController.getUsersWithCustomRates
);

/**
 * @route   GET /api/v1/admin/config/user-commission-rate/:userId
 * @desc    Get user's commission rate
 * @access  Private (Admin)
 */
router.get(
  '/config/user-commission-rate/:userId',
  authenticateAdmin,
  requireRole(['super_admin', 'admin']),
  adminCommissionController.getUserCommissionRate
);

/**
 * @route   PUT /api/v1/admin/config/user-commission-rate/:userId
 * @desc    Set user's commission rate
 * @access  Private (Super Admin only)
 */
router.put(
  '/config/user-commission-rate/:userId',
  authenticateAdmin,
  requireRole(['super_admin']),
  logAdminOperation,
  adminCommissionController.setUserCommissionRate
);

module.exports = router;
