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


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

exports.tradeOption = asyncHandler(async (req, res) => {
  const userId = req.user.id;
  const result = await optionService.tradeOption({
    userId,
    payload: req.body,
  });
  ApiResponse.success(res, result, "Option order placed successfully")
});

exports.getOptionChain = asyncHandler(async (req, res) => {
  const { symbol } = req.params;
  const result = await optionService.getOptionChain({ symbol });
  ApiResponse.success(res, result)
});

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

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

exports.closeOptionPosition = asyncHandler(async (req, res) => {
  const userId = req.user.id;
  const { positionId } = req.params;
  const result = await optionService.closeOptionPosition({
    userId,
    positionId,
  });
  ApiResponse.success(res, result, "Position closed successfully")
});

exports.cancelOptionOrder = asyncHandler(async (req, res) => {
  const userId = req.user.id;
  const { orderId } = req.params;
  const result = await optionService.cancelOptionOrder({
    userId,
    orderId,
  });
ApiResponse.success(res, result, "Order cancelled successfully")
});