<?php
include __DIR__ . '/includes/header.php';
include __DIR__ . '/includes/nav.php';

$slug = trim($_GET['slug'] ?? '');
$slug = preg_replace('/[^a-zA-Z0-9\-]/', '', $slug);

$dataFile = __DIR__ . '/data/pages.json';
$pages = [];
if (file_exists($dataFile)) {
  $pages = json_decode(file_get_contents($dataFile), true);
  if (!is_array($pages)) $pages = [];
}

$page = $slug !== '' && isset($pages[$slug]) ? $pages[$slug] : null;

$title = $page['title'] ?? 'Page';
$published = !empty($page['published']);

?>
<main class="content-area">
  <div class="smurf-divider"></div>
  <div class="content-wrapper">
    <section class="content-card">
      <h1 class="page-title"><?= htmlspecialchars($title, ENT_QUOTES, 'UTF-8') ?></h1>

      <?php if (!$page): ?>
        <p>This page does not exist.</p>
      <?php elseif (!$published): ?>
        <p>This page is not published.</p>
      <?php else: ?>
        <div class="rich-content">
          <?= $page['content'] ?? '' ?>
        </div>
      <?php endif; ?>

    </section>
  </div>
</main>

<?php include __DIR__ . '/includes/footer.php'; ?>
