const tradeService = require('../services/tradeService');
const ApiResponse = require('../utils/apiResponse');
const { asyncHandler } = require('../middleware/errorHandler');

exports.getOverview = asyncHandler(async (req, res) => {
  const overview = await tradeService.getOverview(req.user.id);
  ApiResponse.success(res, overview );
})

exports.getAllHistory = asyncHandler(async (req, res) => {
  const overview = await tradeService.getAllHistory(req.user.id);
  ApiResponse.success(res, overview );
})

exports.getTerminal = asyncHandler(async (req, res) => {
  const terminalData = await tradeService.getTerminalData(req.user.id);
  //const terminalData = await tradeService.getAllTerminalOrders(req.user.id, req.query);
  ApiResponse.success(res, terminalData );
})

exports.getAllAsset = asyncHandler(async (req, res) => {
  const assets = await tradeService.getAllAsset(req.user.id);
  ApiResponse.success(res, assets );
})

exports.getTerminalHistory = asyncHandler(async (req, res) => {
  //const terminalData = await tradeService.getTerminalHistory(req.user.id);
  const terminalData = await tradeService.getAllOrders(req.user.id, req.query);
  ApiResponse.success(res, terminalData );
})

exports.closeTerminalTrade = asyncHandler(async (req, res) => {
  //const terminalData = await tradeService.getTerminalHistory(req.user.id);
  const {tradeId} = req?.body
  const terminalData = await tradeService.closeTerminalTrade(req.user.id, req?.body?.positionId);
  ApiResponse.success(res, terminalData );
})


exports.getOrders = asyncHandler(async (req, res) => {
  const result = await tradeService.getOrders(req.user.id, req.query);
  ApiResponse.success(res, result);
});

exports.getOpenOrders = asyncHandler(async (req, res) => {
  const { market, symbol } = req.query;
  const orders = await tradeService.getOpenOrders(req.user.id, market, symbol);
  ApiResponse.success(res,  orders );
});

exports.getSpotOrders = asyncHandler(async (req, res) => {
  const result = await tradeService.getSpotOrders(req.user.id, req.query);
  ApiResponse.success(res, result);
});

exports.getOptionOrders = asyncHandler(async (req, res) => {
  const result = await tradeService.getOptionTradeHistory(req.user.id, req.query);
  ApiResponse.success(res, result);
});

exports.getOpenOptionOrders = asyncHandler(async (req, res) => {
  const result = await tradeService.getOpenOptionTrades(req.user.id, req.query);
  ApiResponse.success(res, result);
});


exports.tradeTerminal  = asyncHandler(async (req, res) => {
  const terminals = await tradeService.tradeTerminal(req.user.id, req.user.currency, req.body);
  ApiResponse.success(res, terminals );
})

exports.getMarketsByType = asyncHandler(async (req, res) => {
  const { type } = req.params;
  const markets = await tradeService.getMarketsByType(type);
  ApiResponse.success(res, markets );
})
exports.getAssetData = asyncHandler(async (req, res) => {
  const markets = await tradeService.getAssetData(req.user.id);
  ApiResponse.success(res, markets );
})
exports.getSpotData = asyncHandler(async (req, res) => {
  const markets = await tradeService.getSpotData(req.user.id);
  ApiResponse.success(res, markets );
})
exports.getFxData = asyncHandler(async (req, res) => {
  const markets = await tradeService.getFxData(req.user.id);
  ApiResponse.success(res, markets );
})

exports.tradeAsset = asyncHandler(async (req, res) => {
  const markets = await tradeService.tradeAsset(req.user.id, req.user.currency, req.body);
  ApiResponse.success(res, markets );
})


exports.tradeSpot = asyncHandler(async (req, res) => {
  const result = await tradeService.tradeSpot(req.user.id, req.body);
  ApiResponse.success(res, result);
});

exports.cancelSpotOrder = asyncHandler(async (req, res) => {
  const result = await tradeService.cancelSpotOrder(req.user.id, req.params.id);
  ApiResponse.success(res, result);
});
exports.createOrder = asyncHandler(async (req, res) => {
  const order = await tradeService.createOrder(req.user.id, req.body);
  ApiResponse.created(res, { order }, 'Order created successfully');
});



exports.getOrderById = asyncHandler(async (req, res) => {
  const order = await tradeService.getOrderById(req.user.id, req.params.id);
  ApiResponse.success(res, { order });
});

exports.cancelOrder = asyncHandler(async (req, res) => {
  const order = await tradeService.cancelOrder(req.user.id, req.params.id);
  ApiResponse.success(res, { order }, 'Order cancelled successfully');
});

exports.cancelAllOrders = asyncHandler(async (req, res) => {
  const { market, symbol } = req.query;
  const result = await tradeService.cancelAllOrders(req.user.id, market, symbol);
  ApiResponse.success(res, result, `${result.cancelled} orders cancelled`);
});

exports.getTrades = asyncHandler(async (req, res) => {
  const result = await tradeService.getTrades(req.user.id, req.query);
  ApiResponse.paginated(res, result.trades, result.pagination);
});

exports.getAllOrders = asyncHandler(async (req, res) => {
  const result = await tradeService.getAllOrders(req.user.id, req.query);
  ApiResponse.paginated(res, result.trades, result.pagination);
});

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

exports.getStatistics = asyncHandler(async (req, res) => {
  const { startDate, endDate } = req.query;
  const stats = await tradeService.getStatistics(req.user.id, startDate, endDate);
  ApiResponse.success(res, { statistics: stats });
});

exports.getProfitLoss = asyncHandler(async (req, res) => {
  const { period } = req.query;
  const pnl = await tradeService.getProfitLoss(req.user.id, period);
  ApiResponse.success(res, { pnl });
});
