import { Router } from 'express';
import { TemplateController } from '../controllers/template.controller';
import { authenticateToken } from '../middlewares/auth.middleware';
import { asyncHandler } from '../middlewares/errorHandler.middleware';
import { requireAdmin } from '../middlewares/auth.middleware';

const router = Router();
const templateController = new TemplateController();

/**
 * @route   GET /api/v1/templates
 * @desc    List all templates (with optional filters)
 * @access  Private
 */
router.get('/', authenticateToken, asyncHandler(templateController.listTemplates.bind(templateController)));

/**
 * @route   GET /api/v1/templates/active
 * @desc    Get active templates for workflow creation
 * @access  Private
 */
router.get('/active', authenticateToken, asyncHandler(templateController.getActiveTemplates.bind(templateController)));

/**
 * @route   GET /api/v1/templates/:templateId
 * @desc    Get template by ID
 * @access  Private
 */
router.get('/:templateId', authenticateToken, asyncHandler(templateController.getTemplate.bind(templateController)));

/**
 * @route   POST /api/v1/templates
 * @desc    Create a new template
 * @access  Private (Admin only)
 */
router.post('/', authenticateToken, requireAdmin, asyncHandler(templateController.createTemplate.bind(templateController)));

/**
 * @route   PUT /api/v1/templates/:templateId
 * @desc    Update template
 * @access  Private (Admin only)
 */
router.put('/:templateId', authenticateToken, requireAdmin, asyncHandler(templateController.updateTemplate.bind(templateController)));

/**
 * @route   DELETE /api/v1/templates/:templateId
 * @desc    Delete template (soft delete)
 * @access  Private (Admin only)
 */
router.delete('/:templateId', authenticateToken, requireAdmin, asyncHandler(templateController.deleteTemplate.bind(templateController)));

export default router;

