const commissionService = require('../services/commissionService');
const commissionConfigService = require('../services/commissionConfigService');

/**
 * Admin Commission Controller
 * Handles commission management for admin
 */

/**
 * Get all commissions
 * GET /api/v1/admin/commissions
 */
const getCommissions = async (req, res) => {
  try {
    const { page, limit, inviterId, inviteeId, status } = req.query;

    const result = await commissionService.getAllCommissions({
      page,
      limit,
      inviterId,
      inviteeId,
      status,
    });

    return res.status(200).json({
      code: 0,
      success: true,
      data: result,
    });
  } catch (error) {
    return res.status(500).json({
      code: 500,
      success: false,
      error: {
        code: 'GET_COMMISSIONS_ERROR',
        message: 'Failed to get commissions',
        details: error.message,
      },
    });
  }
};

/**
 * Get commission statistics
 * GET /api/v1/admin/commissions/stats
 */
const getCommissionStats = async (req, res) => {
  try {
    const stats = await commissionService.getPlatformCommissionStats();

    return res.status(200).json({
      code: 0,
      success: true,
      data: stats,
    });
  } catch (error) {
    return res.status(500).json({
      code: 500,
      success: false,
      error: {
        code: 'GET_STATS_ERROR',
        message: 'Failed to get commission statistics',
        details: error.message,
      },
    });
  }
};

/**
 * Get commission rate
 * GET /api/v1/admin/config/commission-rate
 */
const getCommissionRate = async (req, res) => {
  try {
    const config = await commissionConfigService.getCommissionRateConfig();

    return res.status(200).json({
      code: 0,
      success: true,
      data: config,
    });
  } catch (error) {
    return res.status(500).json({
      code: 500,
      success: false,
      error: {
        code: 'GET_RATE_ERROR',
        message: 'Failed to get commission rate',
        details: error.message,
      },
    });
  }
};

/**
 * Set commission rate
 * PUT /api/v1/admin/config/commission-rate
 */
const setCommissionRate = async (req, res) => {
  try {
    const { rate } = req.body;

    if (rate === undefined || rate === null) {
      return res.status(400).json({
        code: 400,
        success: false,
        error: {
          code: 'INVALID_INPUT',
          message: 'Rate is required',
        },
      });
    }

    const result = await commissionConfigService.setCommissionRate(rate);

    return res.status(200).json({
      code: 0,
      success: true,
      data: result,
      message: 'Commission rate updated successfully',
    });
  } catch (error) {
    if (error.message.includes('Invalid commission rate')) {
      return res.status(400).json({
        code: 400,
        success: false,
        error: {
          code: 'INVALID_RATE',
          message: error.message,
        },
      });
    }

    return res.status(500).json({
      code: 500,
      success: false,
      error: {
        code: 'SET_RATE_ERROR',
        message: 'Failed to set commission rate',
        details: error.message,
      },
    });
  }
};

module.exports = {
  getCommissions,
  getCommissionStats,
  getCommissionRate,
  setCommissionRate,
};
