<?php
$pageTitle = 'Search Results: ' . htmlspecialchars($query);
$pageDescription = 'Search results for ' . htmlspecialchars($query) . ' - Find and play free online games on our gaming portal.';
include __DIR__ . '/layout/header.php';
?>

<div class="container mx-auto px-4 py-8">
    <div class="mb-8">
        <nav class="flex mb-4" aria-label="Breadcrumb">
            <ol class="inline-flex items-center space-x-1 md:space-x-3">
                <li class="inline-flex items-center">
                    <a href="/" class="text-gray-400 hover:text-white transition-colors">
                        <i class="fas fa-home mr-2"></i>Home
                    </a>
                </li>
                <li aria-current="page" class="breadcrumb-item">
                    <div class="flex items-center">
                        <i class="fas fa-chevron-right text-gray-400 mx-2 text-xs"></i>
                        <span class="text-white font-medium breadcrumb-text-last">Search Results</span>
                    </div>
                </li>
            </ol>
        </nav>

        <div class="bg-card-bg rounded-xl p-6">
            <h1 class="text-3xl font-bold mb-2">Search Results: "<?= htmlspecialchars($query) ?>"</h1>
            <p class="text-lg text-gray-300">
                Found <?= $totalGames ?> games matching your search query.
            </p>
        </div>
    </div>

    <!-- Search Page Ad -->
    <?php if (isset($settings['enable_ads']) && $settings['enable_ads'] == '1' && isset($settings['search_page_ad']) && !empty($settings['search_page_ad'])): ?>
    <div class="ad-container search-page-ad mb-8">
        <?= $settings['search_page_ad'] ?>
    </div>
    <?php endif; ?>

    <!-- Games Grid -->
    <?php if (count($games) > 0): ?>
    <div class="game-grid mb-8">
        <?php foreach ($games as $game): ?>
        <a href="/game/<?= $game['slug'] ?>" class="game-card group">
            <div class="relative aspect-square overflow-hidden rounded-xl">
                <img src="<?= htmlspecialchars($game['thumbnail']) ?>" alt="<?= htmlspecialchars($game['title']) ?>"
                     class="w-full h-full object-cover transition-transform duration-500 group-hover:scale-110" loading="lazy">
                <div class="absolute top-0 left-0 m-3 z-1">
                    <span class="category-badge">
                        <i class="fas fa-tag mr-1"></i>
                        <?= htmlspecialchars($game['category_name']) ?>
                    </span>
                </div>
                <div class="game-card-overlay">
                    <h3 class="game-card-title"><?= htmlspecialchars($game['title']) ?></h3>
                </div>
                <div class="absolute inset-0 bg-gradient-to-t from-black/80 to-transparent opacity-0 group-hover:opacity-100 transition-opacity flex items-center justify-center">
                    <button class="w-10 h-10 bg-primary rounded-full flex items-center justify-center">
                        <i class="fas fa-play text-white"></i>
                    </button>
                </div>
            </div>
        </a>
        <?php endforeach; ?>
    </div>
    <?php else: ?>
    <div class="bg-card-bg rounded-xl p-8 text-center mb-8">
        <i class="fas fa-search text-5xl text-gray-600 mb-4"></i>
        <h2 class="text-2xl font-bold mb-2">No Games Found</h2>
        <p class="text-gray-400 mb-4">We couldn't find any games matching your search query.</p>
        <div class="flex flex-wrap justify-center gap-4">
            <a href="/" class="btn-primary">
                <i class="fas fa-home mr-2"></i>Return to Home
            </a>
            <a href="/categories" class="btn-secondary">
                <i class="fas fa-th-large mr-2"></i>Browse Categories
            </a>
        </div>
    </div>
    <?php endif; ?>

    <!-- Pagination -->
    <?php if ($totalPages > 1): ?>
    <div class="mt-8 flex justify-center">
        <div class="inline-flex rounded-full overflow-hidden shadow-lg">
            <?php if ($page > 1): ?>
                <a href="?q=<?= urlencode($query) ?>&page=<?= $page - 1 ?>" class="px-4 py-2 text-sm font-medium text-white bg-card-bg hover:bg-card-hover transition-colors">
                    <i class="fas fa-chevron-left mr-1"></i> Previous
                </a>
            <?php else: ?>
                <span class="px-4 py-2 text-sm font-medium text-gray-500 bg-card-bg cursor-not-allowed">
                    <i class="fas fa-chevron-left mr-1"></i> Previous
                </span>
            <?php endif; ?>

            <span class="px-4 py-2 text-sm font-medium text-white bg-primary">
                Page <?= $page ?> of <?= $totalPages ?>
            </span>

            <?php if ($page < $totalPages): ?>
                <a href="?q=<?= urlencode($query) ?>&page=<?= $page + 1 ?>" class="px-4 py-2 text-sm font-medium text-white bg-card-bg hover:bg-card-hover transition-colors">
                    Next <i class="fas fa-chevron-right ml-1"></i>
                </a>
            <?php else: ?>
                <span class="px-4 py-2 text-sm font-medium text-gray-500 bg-card-bg cursor-not-allowed">
                    Next <i class="fas fa-chevron-right ml-1"></i>
                </span>
            <?php endif; ?>
        </div>
    </div>
    <?php endif; ?>

    <!-- Popular Categories -->
    <div class="mt-12">
        <h2 class="section-title">
            <i class="fas fa-th-large"></i>Popular Categories
        </h2>

        <div class="grid grid-cols-2 sm:grid-cols-3 md:grid-cols-4 lg:grid-cols-5 gap-4">
            <?php foreach ($popularCategories as $category): ?>
            <a href="/category/<?= $category['slug'] ?>" class="bg-card-bg hover:bg-card-hover rounded-xl p-4 text-center transition-all">
                <div class="w-12 h-12 bg-primary/20 rounded-full flex items-center justify-center mx-auto mb-3">
                    <i class="fas fa-gamepad text-primary"></i>
                </div>
                <h3 class="font-medium"><?= htmlspecialchars($category['name']) ?></h3>
                <?php if (isset($category['game_count'])): ?>
                <p class="text-sm text-gray-400 mt-1"><?= $category['game_count'] ?> Games</p>
                <?php endif; ?>
            </a>
            <?php endforeach; ?>
        </div>
    </div>
</div>

<?php include __DIR__ . '/layout/footer.php'; ?>
