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

exports.getUsers = asyncHandler(async (req, res) => {
  const data = await adminUserService.getUsers(req.query);
  ApiResponse.success(res, data);
});

exports.getUserById = asyncHandler(async (req, res) => {
  const { userId } = req.params;
  const data = await adminUserService.getUserById(userId);
  ApiResponse.success(res, data);
});

exports.updateUser = asyncHandler(async (req, res) => {
  const { userId } = req.params;
  const user = await adminUserService.updateUser(userId, req.body, req.admin?.id);
  ApiResponse.success(res, { user }, "User updated successfully");
});

exports.suspendUser = asyncHandler(async (req, res) => {
  const { userId } = req.params;
  const user = await adminUserService.suspendUser(userId, req.body, req.admin?.id);
  ApiResponse.success(res, { user }, "User suspended");
});

exports.holdUser = asyncHandler(async (req, res) => {
  const { userId } = req.params;
  const user = await adminUserService.holdUser(userId, req.body, req.admin?.id);
  ApiResponse.success(res, { user }, "User held");
});

exports.disableUser = asyncHandler(async (req, res) => {
  const { userId } = req.params;
  const user = await adminUserService.disableUser(userId, req.body, req.admin?.id);
  ApiResponse.success(res, { user }, "User disabled");
});

exports.updateBalance = asyncHandler(async (req, res) => {
  const { userId } = req.params;
  const result = await adminUserService.updateBalance(userId, req.body, req.admin?.id);
  ApiResponse.success(res, result , "Balance Updated");
});


exports.unsuspendUser = asyncHandler(async (req, res) => {
  const { userId } = req.params;
  const user = await adminUserService.unsuspendUser(userId, req.admin?.id);
  ApiResponse.success(res, { user }, "User unsuspended");
});

exports.getUserTradeHistory = asyncHandler(async (req, res) => {
  const { userId } = req.params;
  const data = await adminUserService.getUserTradeHistory(userId, req.query);
  ApiResponse.success(res, data);
});

exports.addUserTradeHistory = asyncHandler(async (req, res) => {
  const { userId } = req.params;
  const trade = await adminUserService.addUserTradeHistory(userId, req.body, req.admin?.id);
  ApiResponse.success(res, { trade }, "Trade history added");
});

exports.getUserPlanHistory = asyncHandler(async (req, res) => {
  const { userId } = req.params;
  const result = await adminUserService.getUserPlanHistory(userId, req.query);
  ApiResponse.success(res, result);
});

exports.addUserPlanHistory = asyncHandler(async (req, res) => {
  const { userId } = req.params;
  const result = await adminUserService.addUserPlanHistory(userId, req.body, req.admin?.id);
  ApiResponse.success(res, result, "User Plan  created");
});

exports.addUserRoiHistory = asyncHandler(async (req, res) => {
  const { userId } = req.params;
  const trade = await adminUserService.addUserRoiHistory(userId, req.body, req.admin?.id);
  ApiResponse.success(res, trade , "Trade history added");
});

exports.getUserTraderHistory = asyncHandler(async (req, res) => {
  const { userId } = req.params;
  const result = await adminUserService.getUserTraderHistory(userId, req.query);
  ApiResponse.success(res, result);
});

exports.startCopying = asyncHandler(async (req, res) => {
  const { userId , traderId } = req.params;
  const result = await adminUserService.startCopying(userId,traderId, req.body, req.admin?.id);
  ApiResponse.success(res, result, "Trader Copied Successfully");
});

exports.stopCopying = asyncHandler(async (req, res) => {
  const { userId , relationId } = req.params;
  const result = await adminUserService.stopCopying(userId, relationId, req.admin?.id);
  ApiResponse.success(res, result, "Trader stopped  created");
});