<?php

declare(strict_types=1);

require_once dirname(__DIR__, 3) . '/system/bootstrap.php';

myronRequireDesignAccess();

$templateId = trim((string) ($_GET['id'] ?? ''));
$definition = $templateId !== '' ? myronTemplateDefinitionRepository()->findTemplateById($templateId) : null;
$factory = myronTemplatePreviewFactory();
$page = is_array($definition)
    ? $factory->buildPreviewPage($definition)
    : $factory->buildFallbackPage($templateId);

header('Content-Type: text/html; charset=UTF-8');
?>
<!DOCTYPE html>
<html lang="<?= myronEscape(myronSiteLanguage()) ?>" data-myron-preview-document="template">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title><?= myronEscape(myronComposeDocumentTitle('Template Preview')) ?></title>
  <?= myronRenderSiteFaviconLink() . "\n" ?>
  <?= myronRenderDefinitionFontHeadTags() . "\n" ?>
  <link rel="stylesheet" href="<?= myronEscape(myronBuildAssetUrl('runtime/css/tokens.css')) ?>">
  <link rel="stylesheet" href="<?= myronEscape(myronBuildAssetUrl('runtime/css/runtime-fonts.css')) ?>">
  <link rel="stylesheet" href="<?= myronEscape(myronBuildAssetUrl('runtime/css/runtime-token-overrides.css')) ?>">
  <link rel="stylesheet" href="<?= myronEscape(myronBuildAssetUrl('runtime/css/base.css')) ?>">
  <link rel="stylesheet" href="<?= myronEscape(myronBuildAssetUrl('runtime/css/components.css')) ?>">
  <link rel="stylesheet" href="<?= myronEscape(myronBuildAssetUrl('runtime/css/public-site.css')) ?>">
  <link rel="stylesheet" href="<?= myronEscape(myronBuildAssetUrl('runtime/css/style-guide.css')) ?>">
  <script src="<?= myronEscape(myronBuildAssetUrl('runtime/js/interactive-atoms.min.js')) ?>" defer></script>
</head>
<body class="myron-site-page mc-preview-document">
  <main class="myron-site__canvas mc-preview-document__canvas" data-myron-template-preview-document="<?= myronEscape((string) ($page['source_template_id'] ?? $templateId)) ?>">
    <?= myronDefinitionTemplateRenderer()->renderPublishedPage($page, myronPublicBasePath(), []) ?>
  </main>
</body>
</html>
