const router = require("express").Router();
const { authenticateAdmin, audit } = require("../middleware");
const { authLimiter, passwordResetLimiter, verificationLimiter } = require('../middleware/rateLimiter');
const { rules, body, validate } = require("../middleware/validation")
//const audit = require("../middleware/audit");

const AdminAuthController = require("../controllers/admin/adminAuthController");
const AdminUsersController = require("../controllers/admin/adminUsersController");
//const AdminWalletsController = require("../controllers/admin/adminWalletsController");
const AdminTransactionsController = require("../controllers/admin/adminTransactionsController");
const AdminDepositsController = require("../controllers/admin/adminDepositsController");
const AdminWithdrawalsController = require("../controllers/admin/adminWithdrawalsController");
/*const AdminCopyTradersController = require("../controllers/admin/adminCopyTradersController");
const AdminSettingsController = require("../controllers/admin/adminSettingsController"); */

// Auth
router.post("/auth/login", AdminAuthController.login);
router.get("/auth/me", authenticateAdmin, AdminAuthController.me);
router.post(
    '/auth/register',
    authLimiter,
    [
        rules.email,
        rules.password,
        rules.confirm_password,
        body('name').trim().isString().isLength({ max: 50 }),
        body('role').optional()
    ],
    validate,
    AdminAuthController.register
);

router.post(
    '/auth/refresh-token',
    [body('refreshToken').notEmpty().withMessage('Refresh token is required')],
    validate,
    AdminAuthController.refreshToken
);

// Users
router.get("/users", authenticateAdmin, AdminUsersController.getUsers);
router.get("/users/:userId", authenticateAdmin, AdminUsersController.getUserById);
router.patch("/users/:userId", authenticateAdmin, audit("users.update", "user"), AdminUsersController.updateUser);
router.post("/users/:userId/suspend", authenticateAdmin, audit("users.suspend","user"), AdminUsersController.suspendUser);
router.post("/users/:userId/hold", authenticateAdmin, audit("users.suspend","user"), AdminUsersController.holdUser);
router.post("/users/:userId/disable", authenticateAdmin, audit("users.suspend","user"), AdminUsersController.disableUser);
router.post("/users/:userId/activate", authenticateAdmin, audit("users.unsuspend","user"), AdminUsersController.unsuspendUser);
router.post("/users/:userId/update-bal", authenticateAdmin, audit("users.update_balance","user"), AdminUsersController.updateBalance);

// User trade history (admin add)
router.get("/users/:userId/trades", authenticateAdmin, AdminUsersController.getUserTradeHistory);
router.post("/users/:userId/trades", authenticateAdmin, audit("trades.add","trade_history"), AdminUsersController.addUserTradeHistory);
router.get("/users/:userId/plans", authenticateAdmin, AdminUsersController.getUserPlanHistory);
router.post("/users/:userId/plans", authenticateAdmin, audit("plans.add","plan_history"), AdminUsersController.addUserRoiHistory);
router.post("/users/:userId/add-plan", authenticateAdmin, audit("plans.purchase","plan_purchase"), AdminUsersController.addUserPlanHistory);
router.get("/users/:userId/copy-trading", authenticateAdmin, AdminUsersController.getUserTraderHistory);
router.post("/users/:userId/copy/traderId", authenticateAdmin, audit("copy.start","start_copy_trading"), AdminUsersController.startCopying);
router.post("/users/:userId/stop-copy/relationId", authenticateAdmin, audit("copy.stop","stop_copy_trading"), AdminUsersController.stopCopying);

// Wallet registry + actions
/* router.get("/wallets", authenticateAdmin, AdminWalletsController.getWallets);
router.get("/users/:userId/wallets", authenticateAdmin, AdminWalletsController.getUserWallets);
router.post("/wallets", authenticateAdmin, audit("wallets.create","wallet"), AdminWalletsController.createWallet);
router.post("/wallets/:walletId/freeze", authenticateAdmin, audit("wallets.freeze","wallet"), AdminWalletsController.freezeWallet);
router.post("/wallets/:walletId/unfreeze", authenticateAdmin, audit("wallets.unfreeze","wallet"), AdminWalletsController.unfreezeWallet);
router.post("/wallets/:walletId/adjust", authenticateAdmin, audit("wallets.adjust","wallet"), AdminWalletsController.adjustWallet); */

// Unified stream
router.get("/transactions", authenticateAdmin, AdminTransactionsController.getTransactions);
router.get("/transactions/:txId", authenticateAdmin, AdminTransactionsController.getTransactionById);

// Deposits (separate)
router.get("/deposits/funding", authenticateAdmin, (req, res, next) => { req.depositSource = "funding"; next(); }, AdminDepositsController.getDeposits);
router.get("/deposits/wallet", authenticateAdmin, (req, res, next) => { req.depositSource = "wallet"; next(); }, AdminDepositsController.getDeposits);

router.post("/deposits/funding/:depositId/confirm", authenticateAdmin, audit("deposits.funding.confirm","deposit"), (req, res, next) => { req.depositSource = "funding"; next(); }, AdminDepositsController.confirmDeposit);
router.post("/deposits/wallet/:depositId/confirm", authenticateAdmin, audit("deposits.wallet.confirm","deposit"), (req, res, next) => { req.depositSource = "wallet"; next(); }, AdminDepositsController.confirmDeposit);

router.post("/deposits/funding/:depositId/reject", authenticateAdmin, audit("deposits.funding.reject","deposit"), (req, res, next) => { req.depositSource = "funding"; next(); }, AdminDepositsController.rejectDeposit);
router.post("/deposits/wallet/:depositId/reject", authenticateAdmin, audit("deposits.wallet.reject","deposit"), (req, res, next) => { req.depositSource = "wallet"; next(); }, AdminDepositsController.rejectDeposit);

// Withdrawals (separate)
/* router.get("/withdrawals/funding", authenticateAdmin, (req, res, next) => { req.withdrawalSource = "funding"; next(); }, AdminWithdrawalsController.getWithdrawals);
router.get("/withdrawals/wallet", authenticateAdmin, (req, res, next) => { req.withdrawalSource = "wallet"; next(); }, AdminWithdrawalsController.getWithdrawals); */

/* router.post("/withdrawals/funding/:withdrawalId/hold", authenticateAdmin, audit("withdrawals.funding.hold","withdrawal"), (req, res, next) => { req.withdrawalSource = "funding"; next(); }, AdminWithdrawalsController.holdWithdrawal);
router.post("/withdrawals/wallet/:withdrawalId/hold", authenticateAdmin, audit("withdrawals.wallet.hold","withdrawal"), (req, res, next) => { req.withdrawalSource = "wallet"; next(); }, AdminWithdrawalsController.holdWithdrawal); */

/* outer.post("/withdrawals/funding/:withdrawalId/approve", authenticateAdmin, audit("withdrawals.funding.approve","withdrawal"), (req, res, next) => { req.withdrawalSource = "funding"; next(); }, AdminWithdrawalsController.approveWithdrawal);
router.post("/withdrawals/wallet/:withdrawalId/approve", authenticateAdmin, audit("withdrawals.wallet.approve","withdrawal"), (req, res, next) => { req.withdrawalSource = "wallet"; next(); }, AdminWithdrawalsController.approveWithdrawal);

router.post("/withdrawals/funding/:withdrawalId/reject", authenticateAdmin, audit("withdrawals.funding.reject","withdrawal"), (req, res, next) => { req.withdrawalSource = "funding"; next(); }, AdminWithdrawalsController.rejectWithdrawal);
router.post("/withdrawals/wallet/:withdrawalId/reject", authenticateAdmin, audit("withdrawals.wallet.reject","withdrawal"), (req, res, next) => { req.withdrawalSource = "wallet"; next(); }, AdminWithdrawalsController.rejectWithdrawal); */

// Copy traders
/* router.get("/copytraders", authenticateAdmin, AdminCopyTradersController.getCopyTraders);
router.post("/copytraders", authenticateAdmin, audit("copytraders.create","copy_trader"), AdminCopyTradersController.createCopyTrader);
router.patch("/copytraders/:copyTraderId", authenticateAdmin, audit("copytraders.update","copy_trader"), AdminCopyTradersController.updateCopyTrader);
router.post("/copytraders/:copyTraderId/pause", authenticateAdmin, audit("copytraders.pause","copy_trader"), AdminCopyTradersController.pauseCopyTrader);
router.post("/copytraders/:copyTraderId/resume", authenticateAdmin, audit("copytraders.resume","copy_trader"), AdminCopyTradersController.resumeCopyTrader); */

// Settings
/* router.get("/settings", authenticateAdmin, AdminSettingsController.getSettings);
router.put("/settings/:key", authenticateAdmin, audit("settings.set","setting"), AdminSettingsController.setSetting); */

module.exports = router;
