<?php
$pageTitle   = 'dispelled.ca';
$currentPage = 'home';

require_once __DIR__ . '/includes/config.php';
require_once __DIR__ . '/includes/rss.php';

$filter = $_GET['source'] ?? 'all';
$q      = trim($_GET['q'] ?? '');

if (!preg_match('/^[a-z]+$/', $filter)) $filter = 'all';

$feedItems = getAllFeedItems();

if ($q !== '') {
    $filter = 'all';
    $feedItems = array_values(array_filter($feedItems, function($i) use ($q) {
        return stripos($i['title'], $q) !== false
            || stripos($i['description'] ?? '', $q) !== false;
    }));
} elseif ($filter !== 'all') {
    $feedItems = array_values(array_filter($feedItems, fn($i) => $i['source'] === $filter));
}

require_once __DIR__ . '/includes/header.php';
?>

<div class="feed-header">
  <div class="feed-header-row">
    <h1>Terminal</h1>
    <form method="GET" action="" class="search-form">
      <input
        type="search"
        name="q"
        class="search-input"
        placeholder="Search…"
        value="<?= htmlspecialchars($q) ?>"
      >
    </form>
  </div>
  <?php if (!$q): ?>
  <div class="filter-bar">
    <a href="?source=all"       class="filter-btn <?= $filter === 'all'       ? 'active' : '' ?>">All</a>
    <a href="?source=slashdot"  class="filter-btn <?= $filter === 'slashdot'  ? 'active' : '' ?>">Slashdot</a>
    <a href="?source=ars"       class="filter-btn <?= $filter === 'ars'       ? 'active' : '' ?>">Ars Technica</a>
    <a href="?source=hn"        class="filter-btn <?= $filter === 'hn'        ? 'active' : '' ?>">Hacker News</a>
    <a href="?source=register"  class="filter-btn <?= $filter === 'register'  ? 'active' : '' ?>">The Register</a>
    <a href="?source=phoronix"  class="filter-btn <?= $filter === 'phoronix'  ? 'active' : '' ?>">Phoronix</a>
    <a href="?source=lwn"       class="filter-btn <?= $filter === 'lwn'       ? 'active' : '' ?>">LWN.net</a>
    <a href="?source=eff"       class="filter-btn <?= $filter === 'eff'       ? 'active' : '' ?>">EFF</a>
    <a href="?source=krebs"     class="filter-btn <?= $filter === 'krebs'     ? 'active' : '' ?>">Krebs</a>
  </div>
  <?php endif ?>
</div>

<?php if ($q && empty($feedItems)): ?>
  <p class="empty-msg">No results for &ldquo;<?= htmlspecialchars($q) ?>&rdquo;.</p>
<?php elseif (empty($feedItems)): ?>
  <p style="color:var(--text-muted);font-family:'Roboto Mono',monospace;font-size:.85rem;">
    Loading feeds&hellip; refresh in a moment.
  </p>
<?php else: ?>

<ul class="feed-list feed-timeline">
<?php foreach ($feedItems as $item): ?>
  <li class="feed-item">
    <div class="feed-dot source-<?= htmlspecialchars($item['source']) ?>"></div>
    <h2>
      <a href="<?= htmlspecialchars($item['url']) ?>" target="_blank" rel="noreferrer noopener">
        <?= htmlspecialchars($item['title']) ?>
      </a>
    </h2>
    <div class="feed-meta">
      <span class="source-badge <?= htmlspecialchars($item['source']) ?>">
        <?= htmlspecialchars($item['sourceLabel']) ?>
      </span>
      <time class="feed-time"><?= timeAgo($item['publishedAt']) ?></time>
    </div>
    <?php if ($item['description']): ?>
      <p class="feed-desc"><?= htmlspecialchars($item['description']) ?>&hellip;</p>
    <?php endif; ?>
  </li>
<?php endforeach; ?>
</ul>

<?php endif; ?>

<?php require_once __DIR__ . '/includes/footer.php'; ?>
