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

/**
 * Commission Controller
 * Handles commission-related requests for users
 */

/**
 * Get my commissions
 * GET /api/v1/commissions
 */
const getMyCommissions = async (req, res) => {
  try {
    const userId = req.user.id;
    const { page, limit } = req.query;

    const result = await commissionService.getCommissionsByInviter(userId, {
      page,
      limit,
    });

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

/**
 * Get my commission statistics
 * GET /api/v1/commissions/stats
 */
const getMyCommissionStats = async (req, res) => {
  try {
    const userId = req.user.id;

    const stats = await commissionService.getCommissionStats(userId);

    return res.status(200).json({
      success: true,
      data: stats,
    });
  } catch (error) {
    if (error.message === 'User not found') {
      return res.status(404).json({
        success: false,
        error: {
          code: 'USER_NOT_FOUND',
          message: 'User not found',
        },
      });
    }

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

module.exports = {
  getMyCommissions,
  getMyCommissionStats,
};
