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

const forexController = require("../controllers/forexController");
const { authenticate } = require("../middleware/auth");
const { tradingLimiter } = require('../middleware/rateLimiter');
const { validate, rules, query, param, body } = require("../middleware/validation");

router.get('/', authenticate, forexController.getPairs)
router.post('/place', authenticate, forexController.placeTrade)
router.post('/close', authenticate, forexController.closeTrade)
router.post('/fund', authenticate, forexController.fundAccount)
router.post('/debit', authenticate, forexController.debitAccount)

/**
 * Public Market Data (Forex CFD)
 */
/* 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,
  forexController.getPairs
);

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

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

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

router.get(
  "/trades/:symbol",
  [
    param("symbol").trim().toUpperCase(),
    query("limit").optional().isInt({ min: 1, max: 1000 }).toInt(),
  ],
  validate,
  forexController.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,
  forexController.getKlines
);
 */
/**
 * Private Trading (Forex CFD)
 */
/* router.get("/account", auth, forexController.getAccountSummary);

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

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

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

router.post(
  "/orders",
  auth,
  [
    body("symbol").trim().toUpperCase().notEmpty(),
    body("side").isIn(["long","short"]),
    body("type").isIn(["market","limit","stop","stop_limit"]),
    body("qty").isNumeric(),          // could be lots or units depending on your engine
    body("price").optional().isNumeric(),
    body("trigger_price").optional().isNumeric(),
    body("leverage").optional().isInt({ min: 1, max: 500 }).toInt(),
    body("stop_loss").optional().isNumeric(),
    body("take_profit").optional().isNumeric(),
    body("reduce_only").optional().isBoolean(),
    body("client_order_id").optional().isString().trim(),
  ],
  validate,
  forexController.placeOrder
);

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

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

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

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