const {
  getAllUsers,
  getUserById,
  getPlatformStatistics,
  getActiveSwaps,
  getReportedIssues,
  getRecentTransactions,
  getRecentActivity,
  getRecentSignups,
  getRecentListings,
  getCompletedSwaps,
  suspendAccount,
  addAdmin,
  deleteUser,
  removeAdmin,
  promoteAdmin,
  getDashboard,
  adminLogin,
  getUserOrderHistory,
  getUserTransactionHistory,
  getProductByIdAdmin,
  rejectProductListing,
  approveProductListing,
  toggleBadge,
  editAdminDetails,
  getStores,
  updateStore,
  addStore,
  deleteStore,
  getLoggedInAdmin,
  verifyProduct,
  complete2FA,
  getAllOrders,
  addPassword,
} = require('../controllers/admin');
const { authorizeRoles, authenticate } = require('../middleware/auth');
const { Roles } = require('../utils/enum');

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

router.get(
  '/',
  authenticate,
  authorizeRoles([
    Roles.ADMIN,
    Roles.SUPER_ADMIN,
    Roles.SUPPORT_AGENT,
    Roles.VERIFICATION_OFFICER,
  ]),
  getLoggedInAdmin
);

router.get(
  '/users/all',
  authenticate,
  authorizeRoles([
    Roles.SUPER_ADMIN,
    Roles.ADMIN,
    Roles.SUPPORT_AGENT,
    Roles.VERIFICATION_OFFICER,
  ]),
  getAllUsers
);

router.put(
  '/users/toggle-badge',
  authenticate,
  authorizeRoles([Roles.SUPER_ADMIN, Roles.ADMIN, Roles.SUPPORT_AGENT]),
  toggleBadge
);

router.post(
  '/add',
  authenticate,
  authorizeRoles([Roles.SUPER_ADMIN]),
  addAdmin
);

router.post('/login', adminLogin);

router.post('/complete-2fa', complete2FA);

router.get(
  '/get-dashboard',
  authenticate,
  authorizeRoles([Roles.ADMIN, Roles.SUPER_ADMIN]),
  getDashboard
);

router.get(
  '/users/:userId',
  authenticate,
  authorizeRoles([
    Roles.SUPER_ADMIN,
    Roles.ADMIN,
    Roles.SUPPORT_AGENT,
    Roles.VERIFICATION_OFFICER,
  ]),
  getUserById
);

router.get(
  '/users/:userId/order',
  authenticate,
  authorizeRoles([
    Roles.SUPER_ADMIN,
    Roles.ADMIN,
    Roles.SUPPORT_AGENT,
    Roles.VERIFICATION_OFFICER,
  ]),
  getUserOrderHistory
);

router.get(
  '/users/:userId/transaction',
  authenticate,
  authorizeRoles([
    Roles.SUPER_ADMIN,
    Roles.SUPPORT_AGENT,
    Roles.VERIFICATION_OFFICER,
  ]),
  getUserTransactionHistory
);

// Statistics routes
router.get(
  '/statistics',
  authenticate,
  authorizeRoles([Roles.SUPER_ADMIN]),
  getPlatformStatistics
);

// Swap management routes
router.get(
  '/swaps/active',
  authenticate,
  authorizeRoles([Roles.SUPER_ADMIN, Roles.ADMIN, Roles.SUPPORT_AGENT]),
  getActiveSwaps
);

// Issue management routes
router.get(
  '/issues/reported',
  authenticate,
  authorizeRoles([Roles.SUPER_ADMIN, Roles.ADMIN, Roles.SUPPORT_AGENT]),
  getReportedIssues
);

// Transaction routes
router.get(
  '/transactions/recent',
  authenticate,
  authorizeRoles([Roles.SUPER_ADMIN, Roles.ADMIN, Roles.SUPPORT_AGENT]),
  getRecentTransactions
);

// Activity routes
router.get(
  '/activity/recent',
  authenticate,
  authorizeRoles([Roles.SUPER_ADMIN, Roles.ADMIN, Roles.SUPPORT_AGENT]),
  getRecentActivity
);
router.get(
  '/signups/recent',
  authenticate,
  authorizeRoles([Roles.SUPER_ADMIN, Roles.ADMIN, Roles.SUPPORT_AGENT]),
  getRecentSignups
);
router.get(
  '/listings/recent',
  authenticate,
  authorizeRoles([Roles.SUPER_ADMIN, Roles.ADMIN, Roles.SUPPORT_AGENT]),
  getRecentListings
);

router.patch(
  '/product/:productId/verify-product',
  authenticate,
  authorizeRoles([Roles.SUPER_ADMIN, Roles.ADMIN, Roles.SUPPORT_AGENT]),
  verifyProduct
);

router.get(
  '/product/:productId',
  authenticate,
  authorizeRoles([Roles.SUPER_ADMIN, Roles.ADMIN, Roles.SUPPORT_AGENT]),
  getProductByIdAdmin
);

router.get(
  '/swaps/completed',
  authenticate,
  authorizeRoles([Roles.SUPER_ADMIN, Roles.ADMIN, Roles.SUPPORT_AGENT]),
  getCompletedSwaps
);

// Admin management routes
router.put(
  '/users/:userId/suspend',
  authenticate,
  authorizeRoles([Roles.SUPER_ADMIN, Roles.ADMIN, Roles.SUPPORT_AGENT]),
  suspendAccount
);
router.put(
  '/users/:userId/promote',
  authenticate,
  authorizeRoles([Roles.SUPER_ADMIN]),
  promoteAdmin
);
router.put(
  '/users/:userId/demote',
  authenticate,
  authorizeRoles([Roles.SUPER_ADMIN]),
  removeAdmin
);
router.delete(
  '/users/:userId',
  authenticate,
  authorizeRoles([Roles.SUPER_ADMIN, Roles.ADMIN]),
  deleteUser
);

router.put(
  '/product/:productId/approve',
  authenticate,
  authorizeRoles([Roles.SUPER_ADMIN, Roles.ADMIN, Roles.SUPPORT_AGENT]),
  approveProductListing
);

router.put(
  '/product/:productId/reject',
  authenticate,
  authorizeRoles([Roles.SUPER_ADMIN, Roles.ADMIN, Roles.SUPPORT_AGENT]),
  rejectProductListing
);

router.put(
  '/edit-admin',
  authenticate,
  authorizeRoles([
    Roles.SUPER_ADMIN,
    Roles.ADMIN,
    Roles.SUPPORT_AGENT,
    Roles.VERIFICATION_OFFICER,
  ]),
  editAdminDetails
);

router.get(
  '/store',
  authenticate,
  authorizeRoles([Roles.SUPER_ADMIN, Roles.ADMIN]),
  getStores
);

router.patch(
  '/store/:storeId',
  authenticate,
  authorizeRoles([Roles.SUPER_ADMIN, Roles.ADMIN]),
  updateStore
);

router.post(
  '/store',
  authenticate,
  authorizeRoles([Roles.SUPER_ADMIN, Roles.ADMIN]),
  addStore
);

router.delete(
  '/store/:storeId',
  authenticate,
  authorizeRoles([Roles.SUPER_ADMIN, Roles.ADMIN]),
  deleteStore
);

// New route for getting all orders
router.get(
  '/orders/all',
  authenticate,
  authorizeRoles([Roles.SUPER_ADMIN, Roles.ADMIN, Roles.SUPPORT_AGENT]),
  getAllOrders
);

router.post(
  '/add-password',
  authenticate,
  authorizeRoles([Roles.ADMIN]),
  addPassword
);

module.exports = router;
