<?php

session_start();
require_once __DIR__ . '/../../includes/init.php';
require_once __DIR__ . '/../../auth/AuthController.php';

$auth = new AuthController();
if (!$auth->isLoggedIn()) {
    http_response_code(403);
    echo 'Zugriff verweigert.';
    exit;
}

$db = Database::getInstance();
$templateId = $_GET['id'] ?? null;
if (!$templateId || !is_numeric($templateId)) {
    ErrorLogger::log('Ungueltige oder fehlende Template-ID bei Vorschau', 'WARN');
    echo 'Keine gueltige Template-ID uebergeben.';
    exit;
}

try {
    $stmt = $db->prepare('SELECT * FROM email_templates WHERE id = ?');
    $stmt->execute([(int) $templateId]);
    ErrorLogger::logSqlSuccess('Template fuer Vorschau geladen | table: email_templates | context: ' . json_encode(['template_id' => (int) $templateId], JSON_UNESCAPED_UNICODE));
    $template = $stmt->fetch(PDO::FETCH_ASSOC);

    if (!$template) {
        ErrorLogger::log('Template nicht gefunden | context: ' . json_encode(['template_id' => (int) $templateId], JSON_UNESCAPED_UNICODE), 'WARN');
        echo 'Vorlage nicht gefunden.';
        exit;
    }

    $layout = null;
    if (!empty($template['layout_id'])) {
        $layoutStmt = $db->prepare('SELECT * FROM email_layouts WHERE id = ?');
        $layoutStmt->execute([(int) $template['layout_id']]);
        ErrorLogger::logSqlSuccess('Layout fuer Template-Vorschau geladen | table: email_layouts | context: ' . json_encode(['layout_id' => (int) $template['layout_id']], JSON_UNESCAPED_UNICODE));
        $layout = $layoutStmt->fetch(PDO::FETCH_ASSOC) ?: null;
    }

    $html = (string) ($layout['header_html'] ?? '') . (string) ($template['body_html'] ?? '') . (string) ($layout['footer_html'] ?? '');
    ErrorLogger::log('Template-Vorschau geladen | context: ' . json_encode(['template_id' => (int) $templateId], JSON_UNESCAPED_UNICODE), 'INFO');
} catch (Throwable $e) {
    ErrorLogger::logSqlError('Fehler beim Laden der Template-Vorschau | table: email_templates | error: ' . $e->getMessage());
    echo 'Fehler beim Laden der Vorschau.';
    exit;
}
?>
<!DOCTYPE html>
<html lang="de">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Vorschau: <?= htmlspecialchars((string) $template['name'], ENT_QUOTES, 'UTF-8') ?></title>
    <style>
        body {
            margin: 0;
            padding: 24px;
            background: #eef2f7;
            font-family: Arial, Helvetica, sans-serif;
        }
        .preview-stage {
            max-width: 860px;
            margin: 0 auto;
            padding: 20px;
            border-radius: 18px;
            background: #dbe4ee;
        }
        .preview-frame {
            background: #ffffff;
            border: 1px solid #d0d7de;
            border-radius: 18px;
            overflow: hidden;
            box-shadow: 0 18px 60px rgba(15, 23, 42, 0.08);
        }
    </style>
</head>
<body>
    <div class="preview-stage">
        <div class="preview-frame">
            <?= $html ?>
        </div>
    </div>
</body>
</html>
