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

<div class="container page-content">

    <section class="promo-section">
        <div class="promo-card">
            <h3>$0 Delivery Fee</h3>
            <p>On your first order</p>
        </div>
        <div class="promo-card">
            <h3>Earn Rewards</h3>
            <p>With every meal</p>
        </div>
        <div class="promo-card">
            <h3>Support Local</h3>
            <p>Majuro Restaurants</p>
        </div>
    </section>

    <section class="featured-restaurants">
        <h2 class="section-title">Featured Restaurants</h2>
        <div class="restaurant-grid">
            <?php
            require_once 'db/config.php';
            $db = db();
            $stmt = $db->query("SELECT r.id, r.name, r.image_url, AVG(rt.rating) as average_rating 
                                 FROM restaurants r
                                 LEFT JOIN ratings rt ON r.id = rt.restaurant_id
                                 GROUP BY r.id
                                 ORDER BY average_rating DESC, r.id DESC
                                 LIMIT 6");
            $restaurants = $stmt->fetchAll(PDO::FETCH_ASSOC);

            foreach ($restaurants as $restaurant) {
                // Get cuisines for this restaurant
                $cuisine_sql = "SELECT c.name FROM cuisines c JOIN restaurant_cuisines rc ON c.id = rc.cuisine_id WHERE rc.restaurant_id = ?";
                $cuisine_stmt = db()->prepare($cuisine_sql);
                $cuisine_stmt->execute([$restaurant['id']]);
                $restaurant_cuisines_list = $cuisine_stmt->fetchAll(PDO::FETCH_COLUMN);

                echo '<a href="menu.php?restaurant_id=' . htmlspecialchars($restaurant['id']) . '" class="restaurant-card">';
                echo '<div class="card-image" style="background-image: url(\\''.htmlspecialchars($restaurant['image_url'] ? $restaurant['image_url'] : 'assets/images/hero.jpg').'\\\')"></div>';
                echo '<div class="card-content">';
                echo '<h3>' . htmlspecialchars($restaurant['name']) . '</h3>';
                echo '<p class="cuisine-tags">' . htmlspecialchars(implode(', ', $restaurant_cuisines_list)) . '</p>';
                echo '<div class="restaurant-info">';
                if ($restaurant['average_rating']) {
                    echo '<div class="rating-display"><span class="star">★</span> ' . number_format($restaurant['average_rating'], 1) . '</div>';
                } else {
                    echo '<div class="rating-display">New</div>';
                }
                echo '<div class="delivery-info">25-35 min</div>';
                echo '</div>';
                echo '</div>';
                echo '</a>';
            }
            ?>
        </div>
        <div class="see-all-container">
            <a href="index.php" class="see-all-btn">See all restaurants</a>
        </div>
    </section>

</div>

<div id="location-modal" class="modal">
    <div class="modal-content">
        <span class="close-button">&times;</span>
        <h2>Pin Your Location</h2>
        <div id="map"></div>
        <button id="confirm-location" class="btn-primary">Confirm Location</button>
    </div>
</div>

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