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

$slug = isset($_GET['slug']) ? $_GET['slug'] : '';
$game = $slug ? get_game_by_slug($slug) : null;

if (!$game) {
    http_response_code(404);
    $page_title = 'Game not found | ' . SITE_NAME;
    require __DIR__ . '/includes/header.php';
    echo '<div class="container empty-state"><h1>Game not found</h1><p>Try searching instead. <a class="btn btn-primary" href="'.SITE_URL.'/games.php">Search all games</a></p></div>';
    require __DIR__ . '/includes/footer.php';
    exit;
}

// bump view count
$stmt = db()->prepare("UPDATE games SET views = views + 1 WHERE id = ?");
$stmt->execute([$game['id']]);

$page_title = $game['name'] . ' - Play Online / Install | ' . SITE_NAME;
$page_description = mb_strimwidth($game['description'], 0, 155, '...');
$page_url = SITE_URL . '/game-detail.php?slug=' . $game['slug'];

$json_ld = json_encode([
    "@context" => "https://schema.org",
    "@type" => "VideoGame",
    "name" => $game['name'],
    "description" => $game['description'],
    "genre" => $game['category'],
    "applicationCategory" => "Game",
    "url" => $page_url
], JSON_UNESCAPED_SLASHES);

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

$more = search_games('', 8, 0, $game['category']);
?>
<section class="container game-hero">
  <div class="game-thumb"><?= e(mb_substr($game['name'], 0, 2)) ?></div>
  <div>
    <span class="cat"><?= e($game['category']) ?></span>
    <h1><?= e($game['name']) ?></h1>
    <p style="color:var(--text-dim);font-size:1.02rem;max-width:640px"><?= e($game['description']) ?></p>
    <div class="hero-actions" style="margin-top:20px">
      <?php if (!empty($game['play_url'])): ?>
      <a href="<?= e($game['play_url']) ?>" target="_blank" rel="noopener" class="btn btn-primary">Play <?= e($game['name']) ?> Online</a>
      <?php endif; ?>
      <a href="<?= e($game['gplay_url']) ?>" target="_blank" rel="noopener nofollow" class="btn btn-violet">Install on Android</a>
      <a href="<?= e($game['appstore_url']) ?>" target="_blank" rel="noopener nofollow" class="btn btn-ghost">Install on iOS</a>
    </div>
  </div>
</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_INARTICLE) ?>" data-ad-format="auto" data-full-width-responsive="true"></ins>
    <script>(adsbygoogle = window.adsbygoogle || []).push({});</script>
  <?php else: ?>
    Ad slot — in-page unit
  <?php endif; ?>
</div>

<section class="container section">
  <div class="section-head">
    <h2>More <?= e($game['category']) ?> Games</h2>
    <a href="<?= SITE_URL ?>/games.php?category=<?= e(urlencode($game['category'])) ?>" class="see-all">See all &rarr;</a>
  </div>
  <div class="games-grid">
    <?php foreach ($more as $g): if ($g['slug'] === $game['slug']) continue; ?>
    <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'; ?>
