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

// -------- TRADERS --------

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

exports.getTraderById = asyncHandler(async (req, res) => {
  const { id } = req.params;
  const data = await adminCopyTradingService.getTraderById(id);
  ApiResponse.success(res, data);
});

exports.createTrader = asyncHandler(async (req, res) => {
  const trader = await adminCopyTradingService.createTrader(req.body, req.file?.filename ?? undefined, req.admin?.id);
  ApiResponse.success(res, { trader }, "Trader profile created");
});

exports.updateTrader = asyncHandler(async (req, res) => {
  const { id } = req.params;
  const trader = await adminCopyTradingService.updateTrader(id, req.body,req.file?.filename ?? undefined, req.admin?.id);
  ApiResponse.success(res, { trader }, "Trader profile updated");
});

exports.deleteTrader = asyncHandler(async (req, res) => {
  const { id } = req.params;
  const result = await adminCopyTradingService.deleteTrader(id, req.admin?.id);
  ApiResponse.success(res, result, "Trader profile deleted");
});

// -------- RELATIONS --------

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

exports.getRelationById = asyncHandler(async (req, res) => {
  const { id } = req.params;
  const data = await adminCopyTradingService.getRelationById(id);
  ApiResponse.success(res, data);
});

exports.createRelation = asyncHandler(async (req, res) => {
  const relation = await adminCopyTradingService.createRelation(req.body, req.admin?.id);
  ApiResponse.success(res, { relation }, "Copy relation created");
});

exports.updateRelation = asyncHandler(async (req, res) => {
  const { id } = req.params;
  const relation = await adminCopyTradingService.updateRelation(id, req.body, req.admin?.id);
  ApiResponse.success(res, { relation }, "Copy relation updated");
});

exports.deleteRelation = asyncHandler(async (req, res) => {
  const { id } = req.params;
  const result = await adminCopyTradingService.deleteRelation(id, req.admin?.id);
  ApiResponse.success(res, result, "Copy relation deleted");
});

// Quick actions
exports.pauseRelation = asyncHandler(async (req, res) => {
  const { id } = req.params;
  const relation = await adminCopyTradingService.setRelationStatus(id, "paused", req.admin?.id);
  ApiResponse.success(res, { relation }, "Copy relation paused");
});

exports.resumeRelation = asyncHandler(async (req, res) => {
  const { id } = req.params;
  const relation = await adminCopyTradingService.setRelationStatus(id, "active", req.admin?.id);
  ApiResponse.success(res, { relation }, "Copy relation resumed");
});

exports.stopRelation = asyncHandler(async (req, res) => {
  const { id } = req.params;
  const relation = await adminCopyTradingService.setRelationStatus(id, "stopped", req.admin?.id);
  ApiResponse.success(res, { relation }, "Copy relation stopped");
});


// -------- TRADES --------

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

exports.getTradeById = asyncHandler(async (req, res) => {
  const { id } = req.params;
  const data = await adminCopyTradingService.getTradeById(id);
  ApiResponse.success(res, data);
});

exports.createTrade = asyncHandler(async (req, res) => {
  const trade = await adminCopyTradingService.createTrade(req.body, req.admin?.id);
  ApiResponse.success(res, { trade }, "Trade created");
});

exports.updateTrade = asyncHandler(async (req, res) => {
  const { id } = req.params;
  const trade = await adminCopyTradingService.updateTrade(id, req.body, req.admin?.id);
  ApiResponse.success(res, { trade }, "Trade updated");
});

exports.deleteTrade = asyncHandler(async (req, res) => {
  const { id } = req.params;
  const result = await adminCopyTradingService.deleteTrade(id, req.admin?.id);
  ApiResponse.success(res, result, "Trade deleted");
});

exports.pauseTrade = asyncHandler(async (req, res) => {
  const { id } = req.params;
  const trade = await adminCopyTradingService.pauseTrade(id, req.admin?.id);
  ApiResponse.success(res, { trade }, "Trade paused");
});

exports.resumeTrade = asyncHandler(async (req, res) => {
  const { id } = req.params;
  const trade = await adminCopyTradingService.resumeTrade(id, req.admin?.id);
  ApiResponse.success(res, { trade }, "Trade resumed");
});

exports.stopTrade = asyncHandler(async (req, res) => {
  const { id } = req.params;
  const trade = await adminCopyTradingService.stopTrade(id, req.admin?.id);
  ApiResponse.success(res, { trade }, "Trade stopped");
});
