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

$newsDir = __DIR__ . '/data/news/';
$files = glob($newsDir . '*.json') ?: [];
rsort($files);
$posts = [];
foreach ($files as $file) {
    $json = json_decode((string)file_get_contents($file), true);
    if (!is_array($json) || empty($json['published'])) continue;
    $imageVal = trim((string)($json['image'] ?? ''));
    $image = '';
    if ($imageVal !== '') {
        if (preg_match('~^(https?:)?//~i', $imageVal) || strpos($imageVal, '/img/') === 0) $image = $imageVal;
        else $image = '/img/content/' . ltrim($imageVal, '/');
    }
    $posts[] = [
        'title' => (string)($json['title'] ?? ''),
        'author' => (string)($json['author'] ?? ''),
        'date' => (string)($json['date'] ?? ''),
        'tag' => (string)($json['tag'] ?? ''),
        'content' => (string)($json['content'] ?? ''),
        'image' => $image,
    ];
}
?>
<main class="content-area">
  <div class="smurf-divider"></div>
  <div class="content-wrapper">
    <section class="content-card center-text">
      <h1>📰 Latest News</h1>
      <p>Updates, stories, and announcements from Smurfette’s Country Delights</p>
    </section>
  </div>
  <div class="smurf-divider"></div>

  <?php if (empty($posts)): ?>
    <div class="content-wrapper">
      <section class="content-card center-text">
        <p>No news posts available.</p>
      </section>
    </div>
    <div class="smurf-divider"></div>
  <?php else: ?>
    <?php foreach ($posts as $post): ?>
      <div class="content-wrapper">
        <section class="content-card news-post" style="text-align:left;">
          <div style="display:flex;gap:18px;align-items:flex-start;flex-wrap:wrap;">
            <?php if (!empty($post['image'])): ?>
              <div class="news-image" style="flex:0 0 auto;">
                <img src="<?= htmlspecialchars($post['image'], ENT_QUOTES, 'UTF-8') ?>" alt="News Image" style="width:140px;height:180px;object-fit:cover;border-radius:14px;display:block;">
              </div>
            <?php endif; ?>
            <div style="flex:1 1 320px;min-width:0;">
              <h2 class="news-title"><?= htmlspecialchars($post['title'], ENT_QUOTES, 'UTF-8') ?></h2>
              <div class="news-meta">
                <span class="news-author">👤 <?= htmlspecialchars($post['author'], ENT_QUOTES, 'UTF-8') ?></span>
                <span class="news-date">📅 <?= htmlspecialchars($post['date'], ENT_QUOTES, 'UTF-8') ?></span>
                <?php if ($post['tag'] !== ''): ?><span class="news-tag">🏷️ <?= htmlspecialchars($post['tag'], ENT_QUOTES, 'UTF-8') ?></span><?php endif; ?>
              </div>
              <div class="news-content"><?= nl2br(htmlspecialchars($post['content'], ENT_QUOTES, 'UTF-8')) ?></div>
            </div>
          </div>
        </section>
      </div>
      <div class="smurf-divider"></div>
    <?php endforeach; ?>
  <?php endif; ?>
</main>
<?php include __DIR__ . '/includes/footer.php'; ?>
