<?php
$pageTitle='News — Almost Heaven Radio';
include __DIR__ . '/header.php';
include __DIR__ . '/menu.php';
require_once __DIR__ . '/includes/media_helpers.php';
$root = __DIR__;
$items=[];
$newsDir=$root.'/data/news';
if(is_dir($newsDir)){
  foreach(glob($newsDir.'/*.json') ?: [] as $f){
    $j=json_decode((string)file_get_contents($f),true);
    if(!is_array($j) || empty($j['published'])) continue;
    $j['_image_src']=ah_media_src((string)($j['image'] ?? ''), 'img/content');
    $items[]=$j;
  }
  usort($items, fn($a,$b)=>strcmp((string)($b['date'] ?? ''),(string)($a['date'] ?? '')));
}
if(!$items && file_exists($root.'/data/news.txt')){
  $raw=trim((string)file_get_contents($root.'/data/news.txt'));
  foreach(preg_split('/
\s*---\s*
/',$raw) as $chunk){
    $chunk=trim($chunk); if($chunk==='') continue;
    $lines=preg_split('/\R+/',$chunk);
    $items[]=['title'=>trim(strip_tags($lines[0] ?? 'Station update')),'content'=>trim(implode("
",array_slice($lines,1))),'_image_src'=>''];
  }
}
?>
<main class="content-area"><div class="content-wrapper page-shell"><section class="content-card"><h1>📰 Station News</h1><p>Latest updates from Almost Heaven Radio.</p></section><div class="page-shell"><?php foreach($items as $item): ?><article class="content-card"><?php if(!empty($item['_image_src'])): ?><p><img src="<?= htmlspecialchars($item['_image_src']) ?>" alt="<?= htmlspecialchars($item['title'] ?? 'Station update') ?>" style="max-width:260px;width:100%;height:auto;border-radius:12px;"></p><?php endif; ?><h2><?= htmlspecialchars($item['title'] ?? 'Station update') ?></h2><?php if(!empty($item['date'])): ?><p class="text-muted"><?= htmlspecialchars($item['date']) ?></p><?php endif; ?><div class="page-copy" style="white-space:pre-wrap;"><?= htmlspecialchars((string)($item['content'] ?? $item['body'] ?? '')) ?></div></article><?php endforeach; if(!$items): ?><section class="content-card"><p>No news posts yet.</p></section><?php endif; ?></div></div></main>
<?php include __DIR__ . '/footer.php'; ?>
