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

/**
 * Admin Withdrawal Controller
 * Handles withdrawal management HTTP requests for admin panel
 */

/**
 * Get withdrawal list
 * GET /api/v1/admin/withdrawals
 */
const getWithdrawalList = async (req, res) => {
  try {
    const { page, limit, status, search, sortBy, sortOrder } = req.query;

    const result = await adminWithdrawalService.getWithdrawalList({
      page,
      limit,
      status,
      search,
      sortBy,
      sortOrder,
    });

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

/**
 * Get withdrawal details
 * GET /api/v1/admin/withdrawals/:id
 */
const getWithdrawalDetails = async (req, res) => {
  try {
    const { id } = req.params;

    const withdrawal = await adminWithdrawalService.getWithdrawalDetails(id);

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

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

/**
 * Approve withdrawal
 * PUT /api/v1/admin/withdrawals/:id/approve
 */
const approveWithdrawal = async (req, res) => {
  try {
    const { id } = req.params;
    const adminId = req.admin.userId;
    const { notes } = req.body;

    const withdrawal = await adminWithdrawalService.approveWithdrawal(
      id,
      adminId,
      notes
    );

    return res.status(200).json({
      success: true,
      data: withdrawal,
      message: 'Withdrawal approved successfully',
    });
  } catch (error) {
    if (error.message === 'Withdrawal not found') {
      return res.status(404).json({
        success: false,
        error: {
          code: 'WITHDRAWAL_NOT_FOUND',
          message: 'Withdrawal not found',
        },
      });
    }

    if (error.message.includes('Cannot approve withdrawal')) {
      return res.status(400).json({
        success: false,
        error: {
          code: 'INVALID_WITHDRAWAL_STATUS',
          message: error.message,
        },
      });
    }

    if (error.message === 'User has insufficient balance') {
      return res.status(400).json({
        success: false,
        error: {
          code: 'INSUFFICIENT_BALANCE',
          message: 'User has insufficient balance for this withdrawal',
        },
      });
    }

    return res.status(500).json({
      success: false,
      error: {
        code: 'APPROVE_WITHDRAWAL_ERROR',
        message: 'Failed to approve withdrawal',
        details: error.message,
      },
    });
  }
};

/**
 * Reject withdrawal
 * PUT /api/v1/admin/withdrawals/:id/reject
 */
const rejectWithdrawal = async (req, res) => {
  try {
    const { id } = req.params;
    const adminId = req.admin.userId;
    const { reason } = req.body;

    if (!reason || reason.trim() === '') {
      return res.status(400).json({
        success: false,
        error: {
          code: 'REJECTION_REASON_REQUIRED',
          message: 'Rejection reason is required',
        },
      });
    }

    const withdrawal = await adminWithdrawalService.rejectWithdrawal(
      id,
      adminId,
      reason
    );

    return res.status(200).json({
      success: true,
      data: withdrawal,
      message: 'Withdrawal rejected successfully',
    });
  } catch (error) {
    if (error.message === 'Withdrawal not found') {
      return res.status(404).json({
        success: false,
        error: {
          code: 'WITHDRAWAL_NOT_FOUND',
          message: 'Withdrawal not found',
        },
      });
    }

    if (error.message.includes('Cannot reject withdrawal')) {
      return res.status(400).json({
        success: false,
        error: {
          code: 'INVALID_WITHDRAWAL_STATUS',
          message: error.message,
        },
      });
    }

    return res.status(500).json({
      success: false,
      error: {
        code: 'REJECT_WITHDRAWAL_ERROR',
        message: 'Failed to reject withdrawal',
        details: error.message,
      },
    });
  }
};

module.exports = {
  getWithdrawalList,
  getWithdrawalDetails,
  approveWithdrawal,
  rejectWithdrawal,
};
