const express = require('express');
const router = express.Router();
const whatsappTemplateController = require('../controllers/whatsappTemplate.controller');
const { isAuthenticated, allowRoles } = require('../middlewares/authenticate');
const { checkPermission } = require('../middlewares/checkPermission');

// All routes require super admin or staff with permission
router.use(isAuthenticated);
router.use(allowRoles('super-admin', 'staff'), checkPermission("manage_settings"));

// Get all templates (with optional filters)
router.get('/', whatsappTemplateController.getAllTemplates);

// Get template by message type
router.get('/type/:messageType', whatsappTemplateController.getTemplateByType);

// Get single template by ID
router.get('/:id', whatsappTemplateController.getTemplateById);

// Create new template
router.post('/', whatsappTemplateController.createTemplate);

// Update template
router.put('/:id', whatsappTemplateController.updateTemplate);

// Delete template
router.delete('/:id', whatsappTemplateController.deleteTemplate);

// Toggle template active status
router.patch('/:id/toggle', whatsappTemplateController.toggleTemplateStatus);

// Fetch external template details (Auto-Fetch)
router.post('/fetch-external', whatsappTemplateController.fetchExternalTemplate);

// Preview template with variables
router.post('/:id/preview', whatsappTemplateController.previewTemplate);

module.exports = router;
