const express = require('express');
const router = express.Router();
const ctrl = require('../controllers/settings.controller');
const { authenticateToken } = require('../middleware/auth');
const { requireAdmin } = require('../middleware/roleCheck');

router.use(authenticateToken);

// Categories – GET is open to all authenticated users (needed for ticket creation)
router.get('/categories',              ctrl.getCategories);
router.post('/categories',             requireAdmin, ctrl.createCategory);
router.put('/categories/reorder',      requireAdmin, ctrl.reorderCategories);
router.put('/categories/:id',          requireAdmin, ctrl.updateCategory);
router.delete('/categories/:id',       requireAdmin, ctrl.deleteCategory);

// Templates – GET open to all, write admin only
router.get('/templates',               ctrl.getTemplates);
router.post('/templates',              requireAdmin, ctrl.createTemplate);
router.put('/templates/:id',           requireAdmin, ctrl.updateTemplate);
router.delete('/templates/:id',        requireAdmin, ctrl.deleteTemplate);

// Email templates – admin only
router.get('/email-templates',                      requireAdmin, ctrl.getEmailTemplates);
router.put('/email-templates/:type',                requireAdmin, ctrl.updateEmailTemplate);
router.post('/email-templates/:type/reset',         requireAdmin, ctrl.resetEmailTemplate);

// Email design – admin only
router.get('/email-design',                         requireAdmin, ctrl.getEmailDesign);
router.put('/email-design',                         requireAdmin, ctrl.updateEmailDesign);
router.post('/email-design/reset',                  requireAdmin, ctrl.resetEmailDesign);
router.post('/email-preview/:type',                 requireAdmin, ctrl.previewEmail);

module.exports = router;
