import express from 'express';
import { 
  getUsers, 
  getUserStats, 
  getUserProfile, 
  updateUserStatus, 
  updateUserProfileAdmin, 
  updateUserRole,
  getMyViewerProfile,
  updateMyViewerProfile,
  verifyOtp,
  getViewHistory,
  recordViewHistory,
  deleteViewHistoryItem,
  clearAllViewHistory,
  getSavedListings,
  saveListing,
  removeSavedListing,
  getMyReviews
} from '../controllers/userController.js';
import verifyToken from '../middleware/authMiddleware.js';

const router = express.Router();

// ==========================================
// VIEWER / GENERAL USER ROUTES (Self)
// ==========================================
router.get('/me/profile', verifyToken, getMyViewerProfile);
router.put('/me/profile', verifyToken, updateMyViewerProfile);
router.post('/me/verify-otp', verifyToken, verifyOtp);

// View History
router.get('/me/view-history', verifyToken, getViewHistory);
router.post('/me/view-history/:listingId', verifyToken, recordViewHistory);
router.delete('/me/view-history/:listingId', verifyToken, deleteViewHistoryItem);
router.delete('/me/view-history', verifyToken, clearAllViewHistory);

// Saved / Bookmarked Listings
router.get('/me/saved-listings', verifyToken, getSavedListings);
router.post('/me/saved-listings/:listingId', verifyToken, saveListing);
router.delete('/me/saved-listings/:listingId', verifyToken, removeSavedListing);

// User Reviews
router.get('/me/reviews', verifyToken, getMyReviews);

// ==========================================
// ADMINISTRATIVE ROUTES
// ==========================================
router.get('/', verifyToken, getUsers);
router.get('/stats', verifyToken, getUserStats);
router.put('/status', verifyToken, updateUserStatus);
router.put('/admin/:id', verifyToken, updateUserProfileAdmin);
router.put('/role', verifyToken, updateUserRole);
router.get('/:id', verifyToken, getUserProfile);

export default router;
