const router = require("express").Router();
const { authenticateAdmin, audit } = require("../../middleware");
const AdminCopyTradingController = require("../../controllers/admin/copyTradingController");
const createUploader = require("../../middleware/createUploader");

// Trader Profiles CRUD
router.get("/traders", authenticateAdmin, AdminCopyTradingController.listTraders);
router.get("/traders/:id", authenticateAdmin, AdminCopyTradingController.getTraderById);
router.post(
  "/traders",
  authenticateAdmin,
  createUploader({
    folder: 'copytraders',
    fieldName: 'avatar',
    maxSizeMB: 20,
  }),
  audit("copytrading.traders.create", "trader_profile"),
  AdminCopyTradingController.createTrader
);
router.put(
  "/traders/:id",
  authenticateAdmin,
  createUploader({
    folder: 'copytraders',
    fieldName: 'avatar',
    maxSizeMB: 20,
  }),
  audit("copytrading.traders.update", "trader_profile"),
  AdminCopyTradingController.updateTrader
);
router.delete(
  "/traders/:id",
  authenticateAdmin,
  audit("copytrading.traders.delete", "trader_profile"),
  AdminCopyTradingController.deleteTrader
);

// Copy Relations CRUD
router.get("/relations", authenticateAdmin, AdminCopyTradingController.listRelations);
router.get("/relations/:id", authenticateAdmin, AdminCopyTradingController.getRelationById);
router.post(
  "/relations",
  authenticateAdmin,
  audit("copytrading.relations.create", "copy_relation"),
  AdminCopyTradingController.createRelation
);
router.put(
  "/relations/:id",
  authenticateAdmin,
  audit("copytrading.relations.update", "copy_relation"),
  AdminCopyTradingController.updateRelation
);
router.delete(
  "/relations/:id",
  authenticateAdmin,
  audit("copytrading.relations.delete", "copy_relation"),
  AdminCopyTradingController.deleteRelation
);

// Quick actions
router.post(
  "/relations/:id/pause",
  authenticateAdmin,
  audit("copytrading.relations.pause", "copy_relation"),
  AdminCopyTradingController.pauseRelation
);
router.post(
  "/relations/:id/resume",
  authenticateAdmin,
  audit("copytrading.relations.resume", "copy_relation"),
  AdminCopyTradingController.resumeRelation
);
router.post(
  "/relations/:id/stop",
  authenticateAdmin,
  audit("copytrading.relations.stop", "copy_relation"),
  AdminCopyTradingController.stopRelation
);


// Copy Trades CRUD
router.get("/trades", authenticateAdmin, AdminCopyTradingController.listTrades);
router.get("/trades/:id", authenticateAdmin, AdminCopyTradingController.getTradeById);

router.post(
  "/trades",
  authenticateAdmin,
  audit("copytrading.trades.create", "copy_trade"),
  AdminCopyTradingController.createTrade
);

router.put(
  "/trades/:id",
  authenticateAdmin,
  audit("copytrading.trades.update", "copy_trade"),
  AdminCopyTradingController.updateTrade
);

router.delete(
  "/trades/:id",
  authenticateAdmin,
  audit("copytrading.trades.delete", "copy_trade"),
  AdminCopyTradingController.deleteTrade
);

// quick actions
router.post(
  "/trades/:id/pause",
  authenticateAdmin,
  audit("copytrading.trades.pause", "copy_trade"),
  AdminCopyTradingController.pauseTrade
);

router.post(
  "/trades/:id/resume",
  authenticateAdmin,
  audit("copytrading.trades.resume", "copy_trade"),
  AdminCopyTradingController.resumeTrade
);

router.post(
  "/trades/:id/stop",
  authenticateAdmin,
  audit("copytrading.trades.stop", "copy_trade"),
  AdminCopyTradingController.stopTrade
);


module.exports = router;