<?php include 'header.php'; ?>

<div class="container">
    <div class="row align-items-center mb-5 mt-4 mt-lg-5 text-center text-lg-start">
        <div class="col-lg-7">
            <h1 class="display-4 fw-bold mb-3"><?php echo __('hero_title'); ?></h1>
            <p class="lead text-muted mb-4 px-3 px-lg-0"><?php echo __('hero_subtitle'); ?></p>
            
            <div class="d-flex gap-2 justify-content-center justify-content-lg-start">
                <a href="#latest" class="btn btn-success btn-lg px-4 rounded-pill"><?php echo __('explore_apps'); ?></a>
                <a href="/register" class="btn btn-outline-dark btn-lg px-4 rounded-pill border-1"><?php echo __('join_referral'); ?></a>
            </div>
        </div>
        <div class="col-lg-5 d-none d-lg-block text-center">
            <div class="position-relative">
                <div class="position-absolute rounded-circle" style="width: 400px; height: 400px; top: -50px; right: -50px; z-index: -1; filter: blur(60px); opacity: 0.15; animation: color-cycle 20s infinite, floating 10s infinite ease-in-out;"></div>
                <img src="https://img.icons8.com/color/512/android-os.png" class="img-fluid floating-animation" alt="Android APKs" style="max-height: 350px;">
            </div>
        </div>
    </div>

    <!-- Featured Section (Horizontal Scroll on Mobile) -->
    <?php 
        $featuredApks = $db->query("SELECT * FROM apks WHERE is_vip = 1 LIMIT 10")->fetchAll();
        if ($featuredApks):
    ?>
    <section class="mb-5">
        <h4 class="fw-bold mb-3 d-flex align-items-center">
            <i class="bi bi-fire text-danger me-2"></i> Featured Apps
        </h4>
        <div class="d-flex overflow-auto pb-3 featured-scroll" style="scrollbar-width: none; -ms-overflow-style: none;">
            <?php foreach ($featuredApks as $f): ?>
                <div class="flex-shrink-0 me-3" style="width: 130px;">
                    <a href="/apk/<?php echo $f['slug']; ?>" class="text-decoration-none">
                        <div class="card border-0 shadow-sm rounded-4 text-center p-2 h-100 hover-lift">
                            <img src="<?php echo !empty($f['icon_path']) ? '/'.$f['icon_path'] : $f['image_url']; ?>" class="rounded-3 mx-auto mb-2 shadow-sm" width="56" height="56" style="object-fit: cover;">
                            <h6 class="card-title fw-bold mb-0 text-truncate small" style="font-size: 0.75rem;"><?php echo $f['title']; ?></h6>
                            <span class="x-small text-muted" style="font-size: 0.65rem;"><?php echo $f['version']; ?></span>
                        </div>
                    </a>
                </div>
            <?php endforeach; ?>
        </div>
    </section>
    <style>.featured-scroll::-webkit-scrollbar { display: none; }</style>
    <?php endif; ?>

    <section id="latest" class="mb-5">
        <div class="d-flex justify-content-between align-items-center mb-3">
            <h2 class="fw-bold mb-0 h4" id="latest-title">
                <?php if (!empty($_GET['search'])): ?>
                    <?php echo __('search_results_for', 'Search results for'); ?>: "<?php echo htmlspecialchars($_GET['search']); ?>"
                <?php else: ?>
                    <?php echo __('latest_apks'); ?>
                <?php endif; ?>
            </h2>
            <div class="dropdown d-none d-md-block">
                <button class="btn btn-white shadow-sm border rounded-pill dropdown-toggle btn-sm" type="button" data-bs-toggle="dropdown" id="category-dropdown-btn">
                    <?php echo __('categories'); ?>
                </button>
                <ul class="dropdown-menu dropdown-menu-end shadow border-0" id="category-menu">
                    <li><a class="dropdown-item category-filter" href="/" data-category=""><?php echo __('all_categories'); ?></a></li>
                    <?php 
                    $categories = $db->query("SELECT * FROM categories")->fetchAll();
                    foreach ($categories as $cat): ?>
                        <li><a class="dropdown-item category-filter" href="/?category=<?php echo $cat['slug']; ?>" data-category="<?php echo $cat['slug']; ?>"><?php echo $cat['name']; ?></a></li>
                    <?php endforeach; ?>
                </ul>
            </div>
        </div>

        <!-- Category Chips for Mobile & Quick Filter -->
        <div class="d-flex overflow-auto pb-3 mb-4 category-scroll" style="scrollbar-width: none; -ms-overflow-style: none;">
            <a href="/" class="btn <?php echo !isset($_GET['category']) ? 'btn-success' : 'btn-light'; ?> rounded-pill px-4 me-2 flex-shrink-0 btn-sm shadow-sm ajax-cat-link" data-category=""><?php echo __('all'); ?></a>
            <?php foreach ($categories as $cat): ?>
                <?php $isActive = isset($_GET['category']) && $_GET['category'] == $cat['slug']; ?>
                <a href="/?category=<?php echo $cat['slug']; ?>" class="btn <?php echo $isActive ? 'btn-success' : 'btn-light'; ?> rounded-pill px-4 me-2 flex-shrink-0 btn-sm shadow-sm ajax-cat-link" data-category="<?php echo $cat['slug']; ?>"><?php echo $cat['name']; ?></a>
            <?php endforeach; ?>
        </div>
        <style>.category-scroll::-webkit-scrollbar { display: none; }</style>
        
        <div id="apk-grid-container">
            <?php include 'partials/apk_list.php'; ?>
        </div>
    </section>

    <!-- Referral Banner -->
    <div class="bg-dark text-white p-4 p-md-5 rounded-5 mt-5 mb-5 shadow-lg position-relative overflow-hidden">
        <div class="position-absolute bg-success opacity-10 rounded-circle" style="width: 300px; height: 300px; bottom: -100px; left: -100px; filter: blur(50px);"></div>
        
        <div class="row align-items-center text-center text-lg-start position-relative">
            <div class="col-lg-8">
                <h2 class="fw-bold mb-3 h3"><?php echo __('referral_journey_title'); ?></h2>
                <p class="mb-0 text-white-50 small"><?php echo __('referral_journey_text'); ?></p>
            </div>
            <div class="col-lg-4 text-center text-lg-end mt-4 mt-lg-0">
                <a href="/register" class="btn btn-success btn-lg px-5 rounded-pill w-100 w-lg-auto"><?php echo __('get_started'); ?></a>
            </div>
        </div>
    </div>

    <!-- Newsletter Section -->
    <section class="py-5 mb-5">
        <div class="card border-0 shadow-lg rounded-5 overflow-hidden">
            <div class="card-body p-4 p-md-5">
                <div class="row align-items-center">
                    <div class="col-lg-6 mb-4 mb-lg-0">
                        <h3 class="fw-bold mb-2">Subscribe to our Newsletter</h3>
                        <p class="text-muted mb-0">Get notified about the latest APKs and updates directly in your inbox.</p>
                    </div>
                    <div class="col-lg-6">
                        <form action="/api/newsletter/subscribe" method="POST" class="d-flex gap-2 p-1 bg-light rounded-pill border shadow-sm newsletter-form">
                            <input type="email" name="email" class="form-control border-0 bg-transparent px-4 py-2 newsletter-email" placeholder="Enter your email" required>
                            <button type="submit" class="btn btn-success rounded-pill px-4 newsletter-submit-btn">Subscribe</button>
                        </form>
                        <div class="newsletter-msg mt-3 ps-3 small"></div>
                    </div>
                </div>
            </div>
        </div>
    </section>
</div>

<?php include 'footer.php'; ?>