const adminWithdrawalsService = require("../../services/admin/adminWithdrawalsService");
const ApiResponse = require("../../utils/apiResponse");
const { asyncHandler } = require("../../middleware/errorHandler");

exports.getWithdrawals = asyncHandler(async (req, res) => {
  const source = req.withdrawalSource || "funding";
  const data = await adminWithdrawalsService.getWithdrawals(source, req.query);
  ApiResponse.success(res, data);
});

exports.holdWithdrawal = asyncHandler(async (req, res) => {
  const source = req.withdrawalSource || "funding";
  const { withdrawalId } = req.params;
  const data = await adminWithdrawalsService.holdWithdrawal(source, withdrawalId, req.body, req.admin?.id);
  ApiResponse.success(res, data, "Withdrawal held");
});

exports.approveWithdrawal = asyncHandler(async (req, res) => {
  const source = req.withdrawalSource || "funding";
  const { withdrawalId } = req.params;
  const data = await adminWithdrawalsService.approveWithdrawal(source, withdrawalId, req.admin?.id);
  ApiResponse.success(res, data, "Withdrawal approved");
});

exports.rejectWithdrawal = asyncHandler(async (req, res) => {
  const source = req.withdrawalSource || "funding";
  const { withdrawalId } = req.params;
  const data = await adminWithdrawalsService.rejectWithdrawal(source, withdrawalId, req.body, req.admin?.id);
  ApiResponse.success(res, data, "Withdrawal rejected");
});
