import { Router } from 'express';
import { usersController } from './users.controller.js';
import { authenticate, requireRoles } from '../../shared/middleware/auth.middleware.js';

const router = Router();

// All routes require authentication
router.use(authenticate);

// Get current user profile
router.get('/me', (req, res, next) => usersController.getMe(req, res, next));

// List users (admin, manager)
router.get('/', requireRoles('admin', 'manager', 'super_admin'), (req, res, next) =>
  usersController.findAll(req, res, next)
);

// Get user by ID
router.get('/:id', requireRoles('admin', 'manager', 'super_admin'), (req, res, next) =>
  usersController.findById(req, res, next)
);

// Create user (admin only)
router.post('/', requireRoles('admin', 'super_admin'), (req, res, next) =>
  usersController.create(req, res, next)
);

// Update user (admin only)
router.put('/:id', requireRoles('admin', 'super_admin'), (req, res, next) =>
  usersController.update(req, res, next)
);

// Delete user (admin only)
router.delete('/:id', requireRoles('admin', 'super_admin'), (req, res, next) =>
  usersController.delete(req, res, next)
);

// Activate/Deactivate user (admin only)
router.post('/:id/activate', requireRoles('admin', 'super_admin'), (req, res, next) =>
  usersController.activate(req, res, next)
);

router.post('/:id/deactivate', requireRoles('admin', 'super_admin'), (req, res, next) =>
  usersController.deactivate(req, res, next)
);

// User roles
router.get('/:id/roles', requireRoles('admin', 'super_admin'), (req, res, next) =>
  usersController.getRoles(req, res, next)
);

router.post('/:id/roles', requireRoles('admin', 'super_admin'), (req, res, next) =>
  usersController.assignRole(req, res, next)
);

router.delete('/:id/roles/:roleId', requireRoles('admin', 'super_admin'), (req, res, next) =>
  usersController.removeRole(req, res, next)
);

export default router;
