<?php
include 'includes/header.php';
require_once __DIR__ . '/includes/content_store.php';

$items = array_values(array_filter(ht_load_news_items(__DIR__), fn($item) => !empty($item['published'])));
?>
<div class="container content-box mt-5">
  <div class="text-center mb-5"><h1>Station News</h1><p class="subtext">Latest updates from Home Time Radio</p></div>
  <?php foreach($items as $item): ?>
    <?php $imageSrc = ht_asset_url((string)($item['image'] ?? ''), 'img/content'); ?>
    <div class="bg-light p-4 rounded mb-4">
      <div class="row g-3 align-items-start">
        <?php if($imageSrc !== ''): ?>
          <div class="col-md-auto"><img src="<?= htmlspecialchars($imageSrc) ?>" alt="<?= htmlspecialchars((string)($item['title'] ?? 'News image')) ?>" style="width:140px;height:180px;object-fit:cover;border-radius:14px;"></div>
        <?php endif; ?>
        <div class="col">
          <h3><?= htmlspecialchars((string)($item['title'] ?? 'News Update')) ?></h3>
          <div style="opacity:.75;" class="mb-2">
            <?php if(!empty($item['date'])): ?><?= htmlspecialchars((string)$item['date']) ?><?php endif; ?>
            <?php if(!empty($item['author'])): ?><?= !empty($item['date']) ? ' • ' : '' ?><?= htmlspecialchars((string)$item['author']) ?><?php endif; ?>
            <?php if(!empty($item['tag'])): ?><?= (!empty($item['date']) || !empty($item['author'])) ? ' • ' : '' ?><?= htmlspecialchars((string)$item['tag']) ?><?php endif; ?>
          </div>
          <p><?= nl2br(htmlspecialchars((string)($item['content'] ?? ''))) ?></p>
        </div>
      </div>
    </div>
  <?php endforeach; ?>
  <?php if(empty($items)): ?><div class="bg-light p-4 rounded">No news posted yet.</div><?php endif; ?>
</div>
<?php include 'includes/footer.php'; ?>
