<?php
require_once __DIR__ . '/includes/functions.php';

$page_title = SITE_NAME . ' - Online Games News, Updates & Free Play Directory';
$page_description = 'Latest online & mobile game news, patch notes and updates, plus a searchable library of top games with play and install links. Auto-updated around the clock.';
$page_url = SITE_URL . '/index.php';

$json_ld = json_encode([
    "@context" => "https://schema.org",
    "@type" => "WebSite",
    "name" => SITE_NAME,
    "url" => SITE_URL . '/',
    "potentialAction" => [
        "@type" => "SearchAction",
        "target" => SITE_URL . "/games.php?q={search_term_string}",
        "query-input" => "required name=search_term_string"
    ]
], JSON_UNESCAPED_SLASHES);

require __DIR__ . '/includes/header.php';

$latestNews = get_latest_news(9, 0);
$tickerNews = get_latest_news(14, 0);
$popularGames = search_games('', 12, 0);
$categories = get_categories();
?>

<section class="hero container">
  <div class="hero-eyebrow">Live &middot; Auto-updated feed</div>
  <h1>Every online game update, <span>in one place.</span></h1>
  <p class="lead">Fresh patch notes, esports news and mobile game updates fetched around the clock, plus a searchable directory of the games themselves — with direct play &amp; install links.</p>
  <div class="hero-actions">
    <a href="<?= SITE_URL ?>/news.php" class="btn btn-primary">Latest Game News</a>
    <a href="<?= SITE_URL ?>/games.php" class="btn btn-ghost">Search All Games</a>
  </div>

  <?php if ($tickerNews): ?>
  <div class="ticker-wrap">
    <span class="ticker-label">BREAKING</span>
    <div class="ticker-track">
      <ul>
        <?php foreach (array_merge($tickerNews, $tickerNews) as $n): ?>
        <li><a href="<?= SITE_URL ?>/news-detail.php?slug=<?= e($n['slug']) ?>">&#9679; <?= e($n['title']) ?></a></li>
        <?php endforeach; ?>
      </ul>
    </div>
  </div>
  <?php endif; ?>
</section>

<section class="section container">
  <div class="section-head">
    <h2>Latest Game Updates &amp; News</h2>
    <a href="<?= SITE_URL ?>/news.php" class="see-all">All updates &rarr;</a>
  </div>

  <?php if ($latestNews): ?>
  <div class="news-grid">
    <?php foreach ($latestNews as $n): ?>
    <a class="card" href="<?= SITE_URL ?>/news-detail.php?slug=<?= e($n['slug']) ?>">
      <span class="tag"><?= e($n['source']) ?></span>
      <h3><?= e($n['title']) ?></h3>
      <p><?= e(mb_strimwidth(strip_tags($n['summary']), 0, 130, '...')) ?></p>
      <div class="meta"><time><?= e(time_ago($n['published_at'])) ?></time></div>
    </a>
    <?php endforeach; ?>
  </div>
  <?php else: ?>
  <div class="empty-state">
    <p>News feed is warming up. Run <code>cron/fetch_rss.php</code> once (or wait for the first scheduled cron run) to pull in the latest online game updates.</p>
  </div>
  <?php endif; ?>
</section>

<div class="ad-slot container">
  <?php if (ADSENSE_CLIENT_ID): ?>
    <ins class="adsbygoogle" style="display:block" data-ad-client="<?= e(ADSENSE_CLIENT_ID) ?>" data-ad-slot="<?= e(ADSENSE_SLOT_HEADER) ?>" data-ad-format="auto" data-full-width-responsive="true"></ins>
    <script>(adsbygoogle = window.adsbygoogle || []).push({});</script>
  <?php else: ?>
    Ad slot — add your AdSense client &amp; slot ID in includes/config.php
  <?php endif; ?>
</div>

<section class="section container">
  <div class="section-head">
    <h2>Popular Online Games</h2>
    <a href="<?= SITE_URL ?>/games.php" class="see-all">Browse all <?= count_games() ?> games &rarr;</a>
  </div>

  <?php if ($categories): ?>
  <div class="games-toolbar" style="margin-bottom:22px">
    <?php foreach (array_slice($categories, 0, 10) as $c): ?>
      <a href="<?= SITE_URL ?>/games.php?category=<?= e(urlencode($c)) ?>" class="page-btn"><?= e($c) ?></a>
    <?php endforeach; ?>
  </div>
  <?php endif; ?>

  <div class="games-grid">
    <?php foreach ($popularGames as $g): ?>
    <a class="game-card" href="<?= SITE_URL ?>/game-detail.php?slug=<?= e($g['slug']) ?>">
      <div class="game-thumb"><?= e(mb_substr($g['name'], 0, 2)) ?></div>
      <span class="cat"><?= e($g['category']) ?></span>
      <h3><?= e($g['name']) ?></h3>
      <p><?= e($g['description']) ?></p>
    </a>
    <?php endforeach; ?>
  </div>
</section>

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