const router  = require('express').Router();
const multer  = require('multer');
const { authMiddleware, requireRole, requirePermission } = require('../middleware/auth');
const { list, create, duplicate, update, remove, importCSV } = require('../controllers/questionController');

const csvUpload = multer({ storage: multer.memoryStorage(), limits: { fileSize: 2 * 1024 * 1024 } });

router.use(authMiddleware);
router.use(requireRole('admin', 'teacher'));

router.get('/',          list);
router.post('/import',   requirePermission('questions.manage'), csvUpload.single('file'), importCSV);
router.post('/',         requirePermission('questions.manage'), create);
router.post('/:id/copy', requirePermission('questions.manage'), duplicate);
router.put('/:id',       requirePermission('questions.manage'), update);
router.delete('/:id',    requireRole('admin', 'teacher'), requirePermission('questions.delete'), remove);

module.exports = router;
