// routes/admin/adminUserTradingRoutes.js
const router = require("express").Router();
const { authenticateAdmin, audit } = require("../../middleware");

const AdminUserTradingController = require("../../controllers/admin/tradingController");

router.post(
  "/holding/place",
  authenticateAdmin,
  audit("asset.trade.place", "place_holding"),
  AdminUserTradingController.tradeAsset
);

router.post(
  "/derivative/place",
  authenticateAdmin,
  audit("derivative.trade.place", "place_derivative"),
  AdminUserTradingController.tradeForex
);

router.post(
  "/terminal/place",
  authenticateAdmin,
  audit("terminal.trade.place", "place_terminal"),
  AdminUserTradingController.tradeTerminal
);

router.post(
  "/spot/place",
  authenticateAdmin,
  audit("spot.trade.place", "place_spot"),
  AdminUserTradingController.tradeSpot
);

router.post(
  "/option/place",
  authenticateAdmin,
  audit("options.trade.place", "place_options"),
  AdminUserTradingController.tradeOption
);

router.get(
  "/pairs",
  authenticateAdmin,
  AdminUserTradingController.listPairs
);

// ==============================
// PORTFOLIO (HTerminal)
// ==============================
router.get(
  "/:userId/trades",
  authenticateAdmin,
  AdminUserTradingController.listTradesByUser
);

router.get(
  "/trades",
  authenticateAdmin,
  AdminUserTradingController.listTrades
);

router.post(
  "/:userId/terminal/trade",
  authenticateAdmin,
  audit("portfolio.terminal.trade", "terminal_trade"),
  AdminUserTradingController.tradeTerminal
);

router.delete(
  "/trades/delete-terminal/:id",
  authenticateAdmin,
  audit("portfolio.terminal.delete", "asset_delete_terminal"),
  AdminUserTradingController.deleteTradeHistory
);

// ==============================
// Option (HTerminal)
// ==============================
router.get(
  "/:userId/options",
  authenticateAdmin,
  AdminUserTradingController.listUserOptions
);

router.get(
  "/options",
  authenticateAdmin,
  AdminUserTradingController.listOptions
);

router.delete(
  "/options/delete/:id",
  authenticateAdmin,
  audit("portfolio.options.delete", "asset_delete_options"),
  AdminUserTradingController.deleteOptionHistory
);

/* router.get(
  "/trades",
  authenticateAdmin,
  AdminUserTradingController.listTrades
);

router.post(
  "/:userId/terminal/trade",
  authenticateAdmin,
  audit("portfolio.terminal.trade", "terminal_trade"),
  AdminUserTradingController.tradeTerminal
);

 */


// ==============================
// PORTFOLIO (Holdings / Lots)
// ==============================
router.get(
  "/:userId/portfolio/holdings",
  authenticateAdmin,
  AdminUserTradingController.listHoldingsByUser
);

router.get(
  "/portfolio/holdings",
  authenticateAdmin,
  AdminUserTradingController.listHoldings
);

router.post(
  "/portfolio/add-dividends",
  authenticateAdmin,
  audit("portfolio.dividends.add", "dividends_add"),
  AdminUserTradingController.addDividends
);


router.get(
  "/:userId/portfolio/lots",
  authenticateAdmin,
  AdminUserTradingController.listLots
);

router.get(
  "/portfolio/lots",
  authenticateAdmin,
  AdminUserTradingController.listLots
);

router.post(
  "/:userId/portfolio/trade",
  authenticateAdmin,
  audit("portfolio.stocks.buy", "asset_buy"),
  AdminUserTradingController.tradeStock
);

router.post(
  "/portfolio/add-lots",
  authenticateAdmin,
  audit("portfolio.stocks.buy", "asset_add_lots"),
  AdminUserTradingController.addLots
);

router.delete(
  "/portfolio/delete-holding/:id",
  authenticateAdmin,
  audit("portfolio.stocks.delete", "asset_delete_holdings"),
  AdminUserTradingController.deleteHolding
);

// admin-only adjustment (audited)
router.post(
  "/:userId/portfolio/holdings/adjust",
  authenticateAdmin,
  audit("portfolio.holdings.adjust", "asset_holding"),
  AdminUserTradingController.adjustHolding
);

// ==============================
// SPOT (Orders / Trades)
// ==============================

router.get(
  "/spot/orders",
  authenticateAdmin,
  AdminUserTradingController.listSpotOrders
);

router.get(
  "/:userId/spot/orders",
  authenticateAdmin,
  AdminUserTradingController.listSpotOrdersByUser
);

router.post(
  "/:userId/spot/orders/:orderId/cancel",
  authenticateAdmin,
  audit("spot.orders.cancel", "spot_order"),
  AdminUserTradingController.cancelSpotOrder
);

router.get(
  "/spot/trades",
  authenticateAdmin,
  AdminUserTradingController.listSpotTrades
);

router.get(
  "/:userId/spot/trades",
  authenticateAdmin,
  AdminUserTradingController.listSpotTradesByUser
);

// ==============================
// DERIVATIVES (Accounts / Orders / Positions / Trades)
// ==============================

router.get(
  "/derivatives/accounts",
  authenticateAdmin,
  AdminUserTradingController.listDerivativeAccounts
);

router.get(
  "/:userId/derivatives/accounts",
  authenticateAdmin,
  AdminUserTradingController.listDerivativeAccountsByUser
);

router.post(
  "/:userId/derivatives/accounts/:accountId/restrict",
  authenticateAdmin,
  audit("derivatives.accounts.restrict", "derivative_account"),
  AdminUserTradingController.restrictDerivativeAccount
);

router.post(
  "/:userId/derivatives/accounts/:accountId/activate",
  authenticateAdmin,
  audit("derivatives.accounts.activate", "derivative_account"),
  AdminUserTradingController.activateDerivativeAccount
);

router.get(
  "/derivatives/orders",
  authenticateAdmin,
  AdminUserTradingController.listDerivativeOrders
);

router.get(
  "/:userId/derivatives/orders",
  authenticateAdmin,
  AdminUserTradingController.listDerivativeOrdersByUser
);


router.post(
  "/:userId/derivatives/orders/:orderId/cancel",
  authenticateAdmin,
  audit("derivatives.orders.cancel", "derivative_order"),
  AdminUserTradingController.cancelDerivativeOrder
);

router.get(
  "/:userId/derivatives/positions",
  authenticateAdmin,
  AdminUserTradingController.listDerivativePositionsByUser
);

router.get(
  "/derivatives/positions",
  authenticateAdmin,
  AdminUserTradingController.listDerivativePositions
);


router.delete(
  "/derivatives/positions/delete/:id",
  authenticateAdmin,
  AdminUserTradingController.deleteDerivativePositions
);

router.get(
  "/derivatives/trades",
  authenticateAdmin,
  AdminUserTradingController.listDerivativeTradesByUser
);

router.get(
  "/:userId/derivatives/trades",
  authenticateAdmin,
  AdminUserTradingController.listDerivativeTrades
);


// ─── Holdings (Stock/Asset) ───────────────────────────────────────────────────
router.patch(
  "/holdings/:holdingId/edit",
  authenticateAdmin,
  audit("holdings.edit", "asset_holding"),
  AdminUserTradingController.editHolding
);

// ─── Derivative Positions ─────────────────────────────────────────────────────
router.patch(
  "/derivatives/positions/:positionId/edit",
  authenticateAdmin,
  audit("der_positions.edit", "derivative_position"),
  AdminUserTradingController.editDerivativePosition
);

// ─── Spot Orders ──────────────────────────────────────────────────────────────
router.patch(
  "/spot/orders/:orderId/edit",
  authenticateAdmin,
  audit("spot_orders.edit", "spot_order"),
  AdminUserTradingController.editSpotOrder
);

// ─── Terminal / Trade History ─────────────────────────────────────────────────
router.patch(
  "/terminal/:tradeId/edit",
  authenticateAdmin,
  audit("terminal.edit", "trade_history"),
  AdminUserTradingController.editTerminalTrade
);

// ─── Binary Options ───────────────────────────────────────────────────────────
router.patch(
  "/options/:tradeId/edit",
  authenticateAdmin,
  audit("options.edit", "binary_trade"),
  AdminUserTradingController.editBinaryOption
);

// routes/adminUserTradingRoutes.js  (close section)

// ─── Close Derivative Position ────────────────────────────────────────────────
router.patch(
  "/der-positions/:positionId/close",
  authenticateAdmin,
  audit("der_positions.close", "derivative_position"),
  AdminUserTradingController.closeDerivativePosition
);

// ─── Close Terminal Trade ─────────────────────────────────────────────────────
router.patch(
  "/terminal/:tradeId/close",
  authenticateAdmin,
  audit("terminal.close", "trade_history"),
  AdminUserTradingController.closeTerminalTrade
);

// ─── Settle Binary Option ─────────────────────────────────────────────────────
router.patch(
  "/options/:tradeId/close",
  authenticateAdmin,
  audit("options.close", "binary_trade"),
  AdminUserTradingController.settleBinaryOption
);

router.get(
  "/all/get",
  authenticateAdmin,
  AdminUserTradingController.allTradingData
);



module.exports = router;

/**
 * HOW TO USE:
 * In your main admin router (like adminS.js), mount this router:
 *   const AdminUserTradingRoutes = require("./adminUserTradingRoutes");
 *   router.use("/", AdminUserTradingRoutes);
 */