<?php
$pageTitle='News — Almost Heaven Radio';
include __DIR__ . '/../header.php';
include __DIR__ . '/../menu.php';
require_once __DIR__ . '/../includes/media_helpers.php';
$posts = [];
$newsDir = __DIR__ . '/../data/news/';
$files = glob($newsDir . '*.json') ?: [];
rsort($files);
foreach ($files as $file) {
  $json = json_decode((string)file_get_contents($file), true);
  if (!is_array($json) || empty($json['published'])) continue;
  $posts[] = [
    'title' => trim((string)($json['title'] ?? 'News Post')),
    'author' => trim((string)($json['author'] ?? '')),
    'date' => trim((string)($json['date'] ?? '')),
    'tag' => trim((string)($json['tag'] ?? '')),
    'content' => trim((string)($json['content'] ?? '')),
    'image' => ah_media_src((string)($json['image'] ?? ''), 'img/content'),
    'featured' => !empty($json['featured_home']),
  ];
}
if (!$posts && file_exists(__DIR__ . '/../data/news.txt')) {
  $raw = trim((string)file_get_contents(__DIR__ . '/../data/news.txt'));
  if ($raw !== '') {
    foreach (preg_split('/
\s*---\s*
/', $raw) as $chunk) {
      $chunk = trim($chunk);
      if ($chunk === '') continue;
      $lines = preg_split('/\R+/', $chunk);
      $posts[] = [
        'title' => trim((string)array_shift($lines)),
        'author' => '', 'date' => '', 'tag' => '', 'image' => '', 'featured' => false,
        'content' => trim(implode("
", $lines)),
      ];
    }
  }
}
?>
<main class="content-area"><div class="content-wrapper page-shell"><section class="content-card"><h1>📰 Station News</h1><p>Catch the latest updates, show announcements, and mountain moments from Almost Heaven Radio.</p></section><?php if (empty($posts)): ?><section class="content-card"><p>No news yet. Stay tuned.</p></section><?php else: foreach ($posts as $post): ?><section class="content-card" style="text-align:left;"><h3><?= !empty($post['featured']) ? '📌 ' : '' ?><?= htmlspecialchars($post['title']) ?></h3><?php if (!empty($post['image'])): ?><p><img src="<?= htmlspecialchars($post['image']) ?>" alt="<?= htmlspecialchars($post['title']) ?>" style="max-width:260px;width:100%;height:auto;border-radius:12px;"></p><?php endif; ?><p class="text-muted"><?php if ($post['author'] !== ''): ?>By <?= htmlspecialchars($post['author']) ?><?php endif; ?><?php if ($post['date'] !== ''): ?><?= $post['author'] !== '' ? ' • ' : '' ?><?= htmlspecialchars($post['date']) ?><?php endif; ?><?php if ($post['tag'] !== ''): ?><?= ($post['author'] !== '' || $post['date'] !== '') ? ' • ' : '' ?><?= htmlspecialchars($post['tag']) ?><?php endif; ?></p><p><?= nl2br(htmlspecialchars($post['content'])) ?></p></section><?php endforeach; endif; ?></div></main>
<?php include __DIR__ . '/../footer.php'; ?>
