<?php

declare(strict_types=1);

require __DIR__ . '/includes/news-storage.php';

$pageTitle = 'News & Changelog | The Unwritten Chapter';
$pageDescription = 'Aktuelle News, Changelogs und Events von The Unwritten Chapter.';
$currentPage = 'news';
$extraStyles = ['news.css'];
$databaseError = '';

try {
    $newsEntries = load_news_entries();
} catch (PDOException $exception) {
    $newsEntries = [];
    $databaseError = 'Datenbankverbindung fehlgeschlagen. Bitte pruefe includes/config.php und database.sql.';
}

function tuc_news_category_class(string $category): string
{
    $category = strtolower(trim($category));

    return match ($category) {
        'changelog' => 'is-changelog',
        'event' => 'is-event',
        'update' => 'is-update',
        default => 'is-news',
    };
}

function tuc_news_date_parts(string $date): array
{
    if ($date === '') {
        return ['--', '--', ''];
    }

    $timestamp = strtotime($date);
    if ($timestamp === false) {
        return [e($date), '', ''];
    }

    return [date('d.m.Y', $timestamp), date('H:i', $timestamp), date('Y', $timestamp)];
}

include __DIR__ . '/includes/header.php';
?>
<main class="news-page">
  <section class="news-hero" aria-labelledby="news-title">
    <div class="news-hero__content">
      <p class="eyebrow">The Unwritten Chapter</p>
      <h1 id="news-title">News &amp; Changelog</h1>
      <p>Alle Server-News, Updates, Changelogs und wichtige Ankündigungen sauber an einem Ort.</p>
    </div>

    <div class="news-hero__panel">
      <span><?= count($newsEntries) ?></span>
      <small>Einträge online</small>
      <?php if (tuc_current_user() !== null && tuc_is_admin(tuc_current_user())): ?>
        <a class="admin-link" href="admin.php">Beitrag erstellen</a>
      <?php endif; ?>
    </div>
  </section>

  <section class="news-board" aria-label="News Timeline">
    <?php if ($databaseError !== ''): ?>
      <article class="empty-state">
        <span class="empty-icon">!</span>
        <h2>Datenbank nicht erreichbar</h2>
        <p><?= e($databaseError) ?></p>
      </article>
    <?php elseif ($newsEntries === []): ?>
      <article class="empty-state">
        <span class="empty-icon">+</span>
        <h2>Noch keine Einträge</h2>
        <p>Sobald News, Changelogs oder Events gepostet werden, erscheinen sie hier.</p>
      </article>
    <?php endif; ?>

    <?php foreach ($newsEntries as $index => $item): ?>
      <?php
        $title = (string) ($item['title'] ?? 'Ohne Titel');
        $category = (string) ($item['category'] ?? 'News');
        $role = (string) ($item['role'] ?? 'Team');
        $author = (string) ($item['author'] ?? 'Unbekannt');
        $content = (string) ($item['content'] ?? '');
        [$dateLabel, $timeLabel] = tuc_news_date_parts((string) ($item['date'] ?? ''));
        $categoryClass = tuc_news_category_class($category);
      ?>
      <article class="news-card <?= e($categoryClass) ?><?= $index === 0 ? ' is-featured' : '' ?>">
        <aside class="news-date" aria-label="Datum">
          <strong><?= e($dateLabel) ?></strong>
          <?php if ($timeLabel !== ''): ?><span><?= e($timeLabel) ?> Uhr</span><?php endif; ?>
        </aside>

        <div class="news-card__body">
          <div class="news-card__top">
            <span class="tag"><?= e($category) ?></span>
            <span class="role">Von <?= e($author) ?> · <?= e($role) ?></span>
          </div>

          <h2><?= e($title) ?></h2>
          <p><?= nl2br(e($content)) ?></p>
        </div>
      </article>
    <?php endforeach; ?>
  </section>
</main>
<?php include __DIR__ . '/includes/footer.php'; ?>
