<?php
declare(strict_types=1);
require_once __DIR__ . '/functions.php';

$slug = trim((string)($_GET['slug'] ?? ''));
$id = trim((string)($_GET['id'] ?? ''));
$events = load_events();
$event = null;

foreach ($events as $e) {
    if ($slug !== '' && !empty($e['slug']) && $e['slug'] === $slug) {
        $event = $e;
        break;
    }
}

if ($event === null && $id !== '') {
    foreach ($events as $e) {
        if (!empty($e['id']) && $e['id'] === $id) {
            $event = $e;
            break;
        }
    }
}

if ($event) {
    $pageTitle = $event['title'] . ' | ' . SITE_NAME;
    $descSource = $event['description'] ?? '';
    if (trim($descSource) === '') {
        $descSource = 'Details zum Termin der ' . SITE_NAME . '.';
    }
    $metaDescription = mb_strimwidth(strip_tags($descSource), 0, 180, '…');
    $canonical = event_url($event);
    $jsonld = render_event_jsonld_graph([$event]);
    $isUpcoming = is_upcoming($event);
} else {
    http_response_code(404);
    $pageTitle = 'Termin nicht gefunden | ' . SITE_NAME;
    $metaDescription = 'Der gesuchte Termin wurde nicht gefunden.';
    $canonical = BASE_URL . '/event.php';
    $jsonld = '';
    $isUpcoming = true;
}
?>
<!doctype html>
<html lang="de">
<head>
  <meta charset="utf-8">
  <title><?= h($pageTitle) ?></title>
  <meta name="description" content="<?= h($metaDescription) ?>">
  <link rel="canonical" href="<?= h($canonical) ?>">
  <meta name="viewport" content="width=device-width, initial-scale=1">

  <meta property="og:title" content="<?= h($pageTitle) ?>">
  <meta property="og:description" content="<?= h($metaDescription) ?>">
  <meta property="og:type" content="website">
  <meta property="og:url" content="<?= h($canonical) ?>">
  <meta property="og:image" content="<?= h($event && !empty($event['image']) ? BASE_URL . $event['image'] : BASE_URL . DEFAULT_OG_IMAGE) ?>">
  <meta name="twitter:card" content="summary_large_image">

  <?php if (!$event): ?>
    <meta name="robots" content="noindex,follow">
  <?php endif; ?>

  <?php if ($jsonld !== ''): ?>
    <script type="application/ld+json"><?= $jsonld ?></script>
  <?php endif; ?>

  <link rel="icon" href="/assets/logo.png" sizes="32x32" type="image/png">
  <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
  <link rel="stylesheet" href="/assets/styles.css">
</head>
<body>

<nav class="navbar navbar-expand-lg bg-body-tertiary border-bottom sticky-top">
  <div class="container">
    <a class="navbar-brand brand-logo" href="index.html">
      <img src="/assets/logo.png" alt="Walschleber KulTour Logo" height="34" width="34">
      <span><?= h(SITE_NAME) ?></span>
    </a>
    <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#nav"><span class="navbar-toggler-icon"></span></button>
    <div id="nav" class="collapse navbar-collapse">
      <ul class="navbar-nav ms-auto">
        <li class="nav-item"><a class="nav-link" href="programm.php">Programm</a></li>
        <li class="nav-item"><a class="nav-link" href="vergangene-vortraege.php">Vergangene Vorträge</a></li>
        <li class="nav-item"><a class="nav-link" href="ueber-uns.html">Über uns</a></li>
        <li class="nav-item"><a class="nav-link" href="kontakt.html">Kontakt</a></li>
      </ul>
    </div>
  </div>
</nav>

<header class="py-5 bg-light border-bottom">
  <div class="container">
    <h1 class="display-6 mb-2"><?= $event ? h($event['title']) : 'Termin nicht gefunden' ?></h1>
    <?php if ($event): ?>
      <p class="text-muted mb-0">
        <?= date('d.m.Y', strtotime($event['date'])) ?> · <?= h($event['time'] ?: '19:30') ?>
        <?php if (!empty($event['speaker'])): ?> · mit <?= h($event['speaker']) ?><?php endif; ?>
      </p>
    <?php else: ?>
      <p class="text-muted mb-0">Bitte prüfe den Link oder schau ins Programm.</p>
    <?php endif; ?>
  </div>
</header>

<main class="container my-5">
  <?php if (!$event): ?>
    <div class="alert alert-warning">Dieser Termin existiert nicht oder wurde entfernt.</div>
    <a class="btn btn-brand" href="programm.php">Zum Programm</a>
  <?php else: ?>
    <div class="row g-4">
      <div class="col-lg-6">
        <div class="p-4 border rounded-3 h-100 bg-white">
          <?php if (!empty($event['image'])): ?>
            <img src="<?= h($event['image']) ?>" alt="<?= h($event['title']) ?>" class="img-fluid rounded mb-3">
          <?php endif; ?>

          <?php if (!empty($event['description'])): ?>
            <div class="mb-0"><?= render_rich_text($event['description']) ?></div>
          <?php else: ?>
            <p class="text-muted mb-0">Details folgen in Kürze.</p>
          <?php endif; ?>
        </div>
      </div>
      <div class="col-lg-6">
        <div class="p-4 border rounded-3 h-100 bg-white">
          <p class="section-kicker mb-1">Auf einen Blick</p>
          <h2 class="h4 mb-3">Termin-Details</h2>
          <ul class="list-unstyled mb-4">
            <li class="mb-2"><strong>Datum:</strong> <?= date('d.m.Y', strtotime($event['date'])) ?></li>
            <li class="mb-2"><strong>Uhrzeit:</strong> <?= h($event['time'] ?: '19:30') ?></li>
            <?php if (!empty($event['speaker'])): ?>
              <li class="mb-2"><strong>Referent:</strong> <?= h($event['speaker']) ?></li>
            <?php endif; ?>
            <?php if (!empty($event['venue_name'])): ?>
              <li class="mb-2"><strong>Ort:</strong> <?= h($event['venue_name']) ?></li>
            <?php endif; ?>
            <?php if (!empty($event['venue_address'])): ?>
              <li class="mb-2"><strong>Adresse:</strong> <?= h($event['venue_address']) ?></li>
            <?php endif; ?>
            <?php if (!empty($event['price'])): ?>
              <li class="mb-2"><strong>Preis:</strong> <?= h($event['price']) ?></li>
            <?php endif; ?>
            <?php if (!empty($event['doors_open'])): ?>
              <li class="mb-2"><strong>Einlass:</strong> <?= h($event['doors_open']) ?></li>
            <?php endif; ?>
            <?php if (!empty($event['tags'])): ?>
              <li class="mb-2"><strong>Tags:</strong> <?= h($event['tags']) ?></li>
            <?php endif; ?>
          </ul>

          <?php if (!empty($event['tickets_info'])): ?>
            <p class="mb-3"><strong>Ticket/VVK:</strong> <?= render_rich_text($event['tickets_info']) ?></p>
          <?php endif; ?>

          <div class="d-flex flex-wrap gap-2">
            <a class="btn btn-brand" href="<?= $isUpcoming ? 'programm.php' : 'vergangene-vortraege.php' ?>">
              <?= $isUpcoming ? 'Zum Programm' : 'Zum Archiv' ?>
            </a>
            <a class="btn btn-outline-brand" href="kontakt.html">Kontakt aufnehmen</a>
          </div>
        </div>
      </div>
    </div>
  <?php endif; ?>
</main>

<footer class="py-4 border-top">
  <div class="container small d-flex gap-3 flex-wrap align-items-center">
    <span>&copy; <?= date('Y') ?> <?= h(SITE_NAME) ?></span>
    <span class="separator">•</span><a href="impressum.html">Impressum</a>
    <span class="separator">•</span><a href="datenschutz.html">Datenschutz</a>
    <span class="separator">•</span>
    <a class="footer-gitea" href="https://git.mrblake.cc/MrBlake/Walschleber-Kultour-CMS" target="_blank" rel="noopener noreferrer" aria-label="Quellcode auf Gitea ansehen" title="Quellcode auf Gitea">
      <img src="/assets/gitea.svg" width="18" height="18" alt="">
    </a>
  </div>
</footer>

<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>
