// src/controllers/perpsController.js
const perpsService = require("../services/perpService");
const ApiResponse = require("../utils/apiResponse");
const { asyncHandler } = require("../middleware/errorHandler");

/**
 * PUBLIC (Market data)
 */
exports.getPairs = asyncHandler(async (req, res) => {
  const result = await perpsService.getPairs(req.query);
  ApiResponse.paginated(res, { pairs: result.pairs }, result.pagination);
});

exports.getTickers = asyncHandler(async (req, res) => {
  const tickers = await perpsService.getTickers(req.query.symbol || null);
  ApiResponse.success(res, { tickers });
});

exports.getTickerBySymbol = asyncHandler(async (req, res) => {
  const ticker = await perpsService.getTickerBySymbol(req.params.symbol);
  ApiResponse.success(res, { ticker });
});

exports.getFunding = asyncHandler(async (req, res) => {
  const funding = await perpsService.getFunding(req.params.symbol);
  ApiResponse.success(res, { funding });
});

exports.getOrderBook = asyncHandler(async (req, res) => {
  const orderbook = await perpsService.getOrderbook(req.params.symbol, req.query.limit);
  ApiResponse.success(res, { orderbook });
});

exports.getRecentTrades = asyncHandler(async (req, res) => {
  const trades = await perpsService.getRecentTrades(req.params.symbol, req.query.limit);
  ApiResponse.success(res, { trades });
});

exports.getKlines = asyncHandler(async (req, res) => {
  const { interval, limit } = req.query;
  const klines = await perpsService.getKlines(req.params.symbol, interval, limit);
  ApiResponse.success(res, { klines });
});

/**
 * PRIVATE (Account/Margin/Leverage/Orders/Positions)
 */
exports.getAccount = asyncHandler(async (req, res) => {
  const data = await perpsService.getAccount(req.user.id);
  ApiResponse.success(res, data);
});

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

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

exports.getPositions = asyncHandler(async (req, res) => {
  const result = await perpsService.getPositions(req.user.id, req.query);
  ApiResponse.paginated(res, { positions: result.positions }, result.pagination);
});

exports.closePosition = asyncHandler(async (req, res) => {
  const result = await perpsService.closePosition(req.user.id, req.params.id);
  ApiResponse.success(res, result);
});

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

exports.getOpenOrders = asyncHandler(async (req, res) => {
  const result = await perpsService.getOpenOrders(req.user.id, req.query);
  ApiResponse.paginated(res, { orders: result.orders }, result.pagination);
});

exports.getOrderHistory = asyncHandler(async (req, res) => {
  const result = await perpsService.getOrderHistory(req.user.id, req.query);
  ApiResponse.paginated(res, { orders: result.orders }, result.pagination);
});

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

exports.getLiquidations = asyncHandler(async (req, res) => {
  const result = await perpsService.getLiquidations(req.user.id, req.query);
  ApiResponse.paginated(res, { liquidations: result.liquidations }, result.pagination);
});

exports.placeOrder = asyncHandler(async (req, res) => {
  const order = await perpsService.placeOrder(req.user.id, req.body);
  ApiResponse.success(res, { order });
});

exports.cancelOrder = asyncHandler(async (req, res) => {
  const result = await perpsService.cancelOrder(req.user.id, req.params.id);
  ApiResponse.success(res, result);
});