const express = require('express');
const router = express.Router();
const { authenticateToken } = require('../middleware/auth');
const { auditLog } = require('../middleware/auditLogger');
const { validateGPS, validateAvailability } = require('../middleware/validator');
const {
  getAllUsers,
  getUserById,
  createUser,
  updateUser,
  deleteUser,
  restoreUser,
  getDeletedUsers,
  getPublicUsers,
  updateAvailability,
  updateGPS,
  exportUsers,
  importUsers,
  bulkUpdateUsers,
  bulkDeleteUsers,
  uploadUserPhoto,
  deleteUserPhoto
} = require('../controllers/userController');

// Public route
router.get('/public/users', getPublicUsers);

// Protected routes (require authentication)
router.get('/users', authenticateToken, getAllUsers);
router.get('/users/export', authenticateToken, auditLog('EXPORT', 'User'), exportUsers);
router.post('/users/import', authenticateToken, auditLog('IMPORT', 'User'), importUsers);
router.get('/users/deleted', authenticateToken, getDeletedUsers);
router.get('/users/:id', authenticateToken, getUserById);
router.post('/users', authenticateToken, auditLog('CREATE', 'User'), createUser);
router.put('/users/:id', authenticateToken, auditLog('UPDATE', 'User'), updateUser);
router.delete('/users/:id', authenticateToken, auditLog('DELETE', 'User'), deleteUser);
router.post('/users/:id/restore', authenticateToken, auditLog('RESTORE', 'User'), restoreUser);
router.put('/users/:id/availability', authenticateToken, auditLog('UPDATE', 'User'), validateAvailability, updateAvailability);
router.put('/users/:id/gps', authenticateToken, auditLog('UPDATE', 'User'), validateGPS, updateGPS);

// User photo
router.post('/users/:id/photo', authenticateToken, auditLog('UPDATE', 'User'), uploadUserPhoto);
router.delete('/users/:id/photo', authenticateToken, auditLog('UPDATE', 'User'), deleteUserPhoto);

// Bulk operations
router.patch('/users/bulk', authenticateToken, auditLog('BULK_UPDATE', 'User'), bulkUpdateUsers);
router.delete('/users/bulk', authenticateToken, auditLog('BULK_DELETE', 'User'), bulkDeleteUsers);

module.exports = router;
