<?php
require_once 'includes/auth.php';
if (!checkPermission('admin')) {
    http_response_code(403);
    exit;
}
require_once 'config/database.php';

$id = filter_input(INPUT_GET, 'id', FILTER_VALIDATE_INT);
if (!$id) {
    echo json_encode(['error' => 'ID manquant']);
    exit;
}

$pdo = getPDO();
$stmt = $pdo->prepare("SELECT contenu_template FROM templates_library WHERE id = ?");
$stmt->execute([$id]);
$template = $stmt->fetch();
if ($template) {
    header('Content-Type: application/json');
    echo $template['contenu_template'];
} else {
    echo json_encode(['error' => 'Template non trouvé']);
}