<?php
if (session_status() === PHP_SESSION_NONE) {
    session_start();
}
include "config/db.php";

// Get featured rental products (only those explicitly marked as featured)
$featured_products = mysqli_query($conn, 
    "SELECT p.*, c.category_name 
     FROM products p 
     LEFT JOIN categories c ON p.category_id = c.id 
     WHERE p.stock > 0 AND p.featured = 1
     ORDER BY p.created_at DESC 
     LIMIT 6"
);

// Get categories (exclude Accessories and Kids Wear)
$categories = mysqli_query($conn, "SELECT * FROM categories WHERE category_name NOT IN ('Accessories','Kids Wear') LIMIT 8");

// Get stats (handle errors)
$total_items_result = mysqli_query($conn, "SELECT COUNT(*) as count FROM products WHERE stock > 0");
$total_items = $total_items_result ? mysqli_fetch_assoc($total_items_result)['count'] : 0;

$happy_customers_result = mysqli_query($conn, "SELECT COUNT(DISTINCT user_id) as count FROM orders");
$happy_customers = $happy_customers_result ? mysqli_fetch_assoc($happy_customers_result)['count'] : 0;

$categories_count_result = mysqli_query($conn, "SELECT COUNT(*) as count FROM categories");
$categories_count = $categories_count_result ? mysqli_fetch_assoc($categories_count_result)['count'] : 0;

include "includes/header.php";
?>

<!-- Hero Section with Video -->
<section class="hero-section">
        <!-- Video Background -->
        <video class="hero-video" autoplay muted loop playsinline>
            <!-- <source src="assets/videos/banner1.mp4" type="video/mp4"> -->
            <source src="assets/videos/banner2.mp4" type="video/mp4">
            Your browser does not support the video tag.
        </video>
        
        <!-- Dark Overlay -->
        <div class="hero-overlay"></div>
        
        <!-- Hero Content -->
        <div class="container hero-content">
            <h1 class="display-4 fw-bold mb-4">Rent Designer Clothes</h1>
            <p class="lead mb-4">Wear your dream outfit without buying it. Rent premium clothing for every occasion.</p>
            
            <?php if (isset($_SESSION['user_name'])): ?>
                <div class="alert alert-light d-inline-block">
                    <i class="fas fa-user-circle me-2"></i>
                    Welcome back, <?php echo htmlspecialchars($_SESSION['user_name']); ?>!
                </div>
            <?php endif; ?>
            
            <div class="mt-4">
                <a href="products.php" class="btn btn-light btn-lg me-3">
                    <i class="fas fa-tshirt me-2"></i>Browse Collection
                </a>
                <a href="#how-it-works" class="btn btn-outline-light btn-lg">
                    <i class="fas fa-play-circle me-2"></i>How It Works
                </a>
            </div>
        </div>
    </section>

<div class="container-fluid px-0">
    
    <!-- Categories -->
    <section class="container py-5">
        <h2 class="text-center mb-5" style="color: #2c3e50;">
            <i class="fas fa-tags me-2"></i>Browse By Category
        </h2>
        
        <!-- Desktop View - Grid -->
        <div class="row d-none d-md-flex">
            <?php while($category = mysqli_fetch_assoc($categories)): ?>
            <div class="col-md-4 col-sm-6 mb-4">
                <a href="products.php?cat=<?php echo $category['id']; ?>" class="text-decoration-none">
                    <div class="card h-100 text-center p-4">
                        <i class="fas fa-tshirt fa-3x mb-3" style="color: #ff6b35;"></i>
                        <h5><?php echo htmlspecialchars($category['category_name']); ?></h5>
                        <p class="text-muted small"><?php echo htmlspecialchars($category['description']); ?></p>
                        <span class="badge bg-primary">Browse</span>
                    </div>
                </a>
            </div>
            <?php endwhile; ?>
        </div>

        <!-- Mobile View - Carousel -->
        <div class="d-md-none">
            <div id="categoryCarousel" class="carousel slide" data-bs-touch="true">
                <div class="carousel-inner">
                    <?php 
                    $cat_array = [];
                    mysqli_data_seek($categories, 0);
                    while($cat = mysqli_fetch_assoc($categories)) {
                        $cat_array[] = $cat;
                    }
                    
                    foreach($cat_array as $index => $category): 
                    ?>
                    <div class="carousel-item <?php echo $index === 0 ? 'active' : ''; ?>">
                        <a href="products.php?cat=<?php echo $category['id']; ?>" class="text-decoration-none">
                            <div class="card text-center p-4" style="min-height: 280px; display: flex; flex-direction: column; justify-content: center;">
                                <i class="fas fa-tshirt fa-3x mb-3" style="color: #ff6b35;"></i>
                                <h5><?php echo htmlspecialchars($category['category_name']); ?></h5>
                                <p class="text-muted small flex-grow-1"><?php echo htmlspecialchars($category['description']); ?></p>
                                <span class="badge bg-primary">Browse</span>
                            </div>
                        </a>
                    </div>
                    <?php endforeach; ?>
                </div>
                
                <!-- Carousel Controls -->
                <button class="carousel-control-prev" type="button" data-bs-target="#categoryCarousel" data-bs-slide="prev">
                    <span class="carousel-control-prev-icon bg-dark rounded-circle p-2"></span>
                </button>
                <button class="carousel-control-next" type="button" data-bs-target="#categoryCarousel" data-bs-slide="next">
                    <span class="carousel-control-next-icon bg-dark rounded-circle p-2"></span>
                </button>
            </div>
        </div>
    </section>

    <!-- Featured Rentals -->
    <?php if(mysqli_num_rows($featured_products) > 0): ?>
    <section class="bg-light py-5">
        <div class="container">
            <h2 class="text-center mb-5" style="color: #2c3e50;">
                <i class="fas fa-crown me-2"></i>Trending Collection
            </h2>
            
            <div class="row">
                <?php while($product = mysqli_fetch_assoc($featured_products)): ?>
                <div class="col-lg-2 col-md-4 col-sm-6 mb-4">
                    <div class="card h-100">
                        <?php
                        // Helper to get first image from JSON array or string
                        $img_file = '';
                        if (!empty($product['images'])) {
                            $decoded = json_decode($product['images'], true);
                            $img_file = is_array($decoded) ? ($decoded[0] ?? '') : $product['images'];
                        }
                        $img_src = !empty($img_file) ? 'assets/images/products/' . $img_file : 'assets/images/products/placeholder.jpg';
                        ?>
                        <img src="<?php echo $img_src; ?>" 
                             class="card-img-top" 
                             alt="<?php echo htmlspecialchars($product['name']); ?>"
                             style="height: 200px; object-fit: cover;"
                             onerror="this.src='assets/images/products/placeholder.jpg'">
                        <div class="card-body">
                            <h6 class="card-title mb-3"><?php echo htmlspecialchars($product['name']); ?></h6>
                            <?php
                            // Base rental price (fallback to price if rental_price is empty/0 for older records)
                            $rental_price = floatval($product['rental_price'] > 0 ? $product['rental_price'] : $product['price']);
                            $discount = intval($product['discount_percent']);
                            
                            // The 'price' column in DB is already the final discounted price saved by the admin panel
                            $final_price = floatval($product['price']);
                            
                            $security = floatval($product['security_deposit']);
                            ?>
                            <div class="price-section">
                                <div class="mb-2">
                                    <small class="text-muted">Rental Price:</small>
                                    <div class="text-primary"><strong>₹<?php echo number_format($rental_price, 2); ?></strong>/day</div>
                                </div>
                                <?php if ($discount > 0): ?>
                                <div class="mb-2">
                                    <span class="badge bg-warning text-dark"><?php echo $discount; ?>% OFF</span>
                                </div>
                                <?php endif; ?>
                                <div class="mb-3">
                                    <strong style="font-size: 1.8rem; color: #28a745;">₹<?php echo number_format($final_price, 2); ?></strong>
                                    <div><small class="text-muted">/day</small></div>
                                </div>
                            </div>
                            <div class="d-grid">
                                <a href="product-details.php?slug=<?php echo $product['slug']; ?>" class="btn btn-sm btn-outline-primary">
                                    <i class="fas fa-eye me-1"></i>View Details
                                </a>
                            </div>
                        </div>
                        
                    </div>
                </div>
                <?php endwhile; ?>
            </div>
            
            <div class="text-center mt-4">
                <a href="products.php" class="btn btn-orange">
                    <i class="fas fa-tshirt me-2"></i>View All Collection
                </a>
            </div>
        </div>
    </section>

    <!-- Stats Section -->
    <section class="container my-5">
        <div class="row text-center">
            <div class="col-md-3 mb-4">
                <div class="dashboard-card h-100">
                    <i class="fas fa-tshirt fa-3x mb-3" style="color: #ff6b35;"></i>
                    <h2 class="fw-bold"><?php echo $total_items; ?>+</h2>
                    <p class="mb-0">Designer Outfits</p>
                </div>
            </div>
            <div class="col-md-3 mb-4">
                <div class="dashboard-card h-100">
                    <i class="fas fa-smile fa-3x mb-3" style="color: #3498db;"></i>
                    <h2 class="fw-bold"><?php echo $happy_customers; ?>+</h2>
                    <p class="mb-0">Happy Customers</p>
                </div>
            </div>
            <div class="col-md-3 mb-4">
                <div class="dashboard-card h-100">
                    <i class="fas fa-tags fa-3x mb-3" style="color: #ff6b35;"></i>
                    <h2 class="fw-bold"><?php echo $categories_count; ?>+</h2>
                    <p class="mb-0">Categories</p>
                </div>
            </div>
            <div class="col-md-3 mb-4">
                <div class="dashboard-card h-100">
                    <i class="fas fa-truck fa-3x mb-3" style="color: #3498db;"></i>
                    <h2 class="fw-bold">24/7</h2>
                    <p class="mb-0">Delivery Support</p>
                </div>
            </div>
        </div>
    </section>

    <!-- How It Works -->
    <section id="how-it-works" class="bg-light py-5">
        <div class="container">
            <h2 class="text-center mb-5" style="color: #2c3e50;">
                <i class="fas fa-play-circle me-2"></i>How ShopNest Works
            </h2>
            
            <div class="row text-center">
                <div class="col-md-4 mb-4">
                    <div class="how-step">
                        <i class="fas fa-search fa-3x mb-3" style="color: #ff6b35;"></i>
                        <h4>Browse & Select</h4>
                        <p>Choose from our curated collection of designer outfits</p>
                    </div>
                </div>
                <div class="col-md-4 mb-4">
                    <div class="how-step">
                        <i class="fas fa-calendar-check fa-3x mb-3" style="color: #3498db;"></i>
                        <h4>Choose Dates</h4>
                        <p>Select your rental period (3-14 days)</p>
                    </div>
                </div>
                <div class="col-md-4 mb-4">
                    <div class="how-step">
                        <i class="fas fa-truck fa-3x mb-3" style="color: #ff6b35;"></i>
                        <h4>Wear & Return</h4>
                        <p>Free delivery, wear it, and we'll pick it up</p>
                    </div>
                </div>
            </div>
        </div>
    </section>

    

    
    <?php endif; ?>

</div> <!-- Close container-fluid -->
    <?php include "includes/footer.php"; ?>
</body>
</html>