
const optionService = require("../services/optionsService");
const { asyncHandler } = require("../middleware/errorHandler");
const ApiResponse = require("../utils/apiResponse");
const { BadRequestError } = require("../utils/errors");


exports.getIndex = asyncHandler(async (req, res) => {
  const userId = req.user.id;
  const result = await optionService.getIndex(userId);
  ApiResponse.success(res, result)
});

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

exports.settleTrade = asyncHandler(async (req, res) => {
  const { id } = req.params;
  if (!req.body?.tradeId) {
    throw new BadRequestError('Provide a  tradeId');
  }
  if (Number(id) !== Number(req.body?.tradeId)) {
    throw new BadRequestError('Invalid tradeId');
  }
  const deposit = await optionService.settleUserTrade(req.user.id, req.body);
  ApiResponse.success(res, deposit)
})