const express = require("express");
const router = express.Router();

const perpController = require("../controllers/perpController");
const { auth } = require("../middleware/auth");
const { validate, rules, query, param, body } = require("../middleware/validation");

/**
 * Public Market Data (Perps)
 */
router.get(
  "/pairs",
  [
    query("base").optional().trim().toUpperCase(),
    query("quote").optional().trim().toUpperCase(),
    query("status").optional().isIn(["active", "inactive", "halted"]),
    rules.page,
    rules.limit,
  ],
  validate,
  perpController.getPairs
);

router.get(
  "/ticker",
  [query("symbol").optional().trim().toUpperCase()],
  validate,
  perpController.getTickers
);

router.get(
  "/ticker/:symbol",
  [param("symbol").trim().toUpperCase()],
  validate,
  perpController.getTickerBySymbol
);

router.get(
  "/orderbook/:symbol",
  [
    param("symbol").trim().toUpperCase(),
    query("limit").optional().isInt({ min: 1, max: 1000 }).toInt(),
  ],
  validate,
  perpController.getOrderBook
);

router.get(
  "/trades/:symbol",
  [
    param("symbol").trim().toUpperCase(),
    query("limit").optional().isInt({ min: 1, max: 1000 }).toInt(),
  ],
  validate,
  perpController.getRecentTrades
);

router.get(
  "/klines/:symbol",
  [
    param("symbol").trim().toUpperCase(),
    query("interval").optional().isIn(["1m","5m","15m","1h","4h","1d","1w"]),
    query("limit").optional().isInt({ min: 1, max: 2000 }).toInt(),
  ],
  validate,
  perpController.getKlines
);

router.get(
  "/funding/:symbol",
  [param("symbol").trim().toUpperCase()],
  validate,
  perpController.getFundingInfo
);

/**
 * Private Trading (Perps)
 */
router.get("/account", auth, perpController.getAccountSummary);

router.post(
  "/settings/margin-mode",
  auth,
  [body("margin_mode").isIn(["cross","isolated"])],
  validate,
  perpController.setMarginMode
);

router.post(
  "/settings/leverage",
  auth,
  [
    body("symbol").trim().toUpperCase().notEmpty(),
    body("leverage").isInt({ min: 1, max: 125 }).toInt(),
    body("margin_mode").optional().isIn(["cross","isolated"]),
  ],
  validate,
  perpController.setLeverage
);

router.get(
  "/positions",
  auth,
  [
    query("symbol").optional().trim().toUpperCase(),
    query("status").optional().isIn(["open","closed","liquidating"]),
    rules.page,
    rules.limit,
  ],
  validate,
  perpController.getPositions
);

router.post(
  "/positions/close/:id",
  auth,
  [param("id").isInt().toInt()],
  validate,
  perpController.closePosition
);

router.post(
  "/positions/:id/tpsl",
  auth,
  [
    param("id").isInt().toInt(),
    body("stop_loss").optional().isNumeric(),
    body("take_profit").optional().isNumeric(),
  ],
  validate,
  perpController.updatePositionTpSl
);

router.get(
  "/orders/open",
  auth,
  [
    query("symbol").optional().trim().toUpperCase(),
    rules.page,
    rules.limit,
  ],
  validate,
  perpController.getOpenOrders
);

router.post(
  "/orders",
  auth,
  [
    body("symbol").trim().toUpperCase().notEmpty(),
    body("side").isIn(["long","short"]),
    body("type").isIn(["limit","market","stop","stop_limit"]),
    body("qty").isNumeric(),
    body("price").optional().isNumeric(),
    body("trigger_price").optional().isNumeric(),
    body("leverage").optional().isInt({ min: 1, max: 125 }).toInt(),
    body("reduce_only").optional().isBoolean(),
    body("position_mode").optional().isIn(["cross","isolated"]),
    body("stop_loss").optional().isNumeric(),
    body("take_profit").optional().isNumeric(),
    body("client_order_id").optional().isString().trim(),
  ],
  validate,
  perpController.placeOrder
);

router.delete(
  "/orders/:id",
  auth,
  [param("id").isInt().toInt()],
  validate,
  perpController.cancelOrder
);

router.get(
  "/orders/history",
  auth,
  [
    query("symbol").optional().trim().toUpperCase(),
    rules.page,
    rules.limit,
  ],
  validate,
  perpController.getOrderHistory
);

router.get(
  "/trades",
  auth,
  [
    query("symbol").optional().trim().toUpperCase(),
    rules.page,
    rules.limit,
  ],
  validate,
  perpController.getMyTrades
);

router.get(
  "/liquidations",
  auth,
  [
    query("symbol").optional().trim().toUpperCase(),
    rules.page,
    rules.limit,
  ],
  validate,
  perpController.getLiquidations
);

module.exports = router;