<?php
require_once 'includes/config.php';
require_once 'includes/functions.php';
require_once 'includes/header.php';
require_once 'includes/marketing_functions.php';

// Get favorite products for logged in users
$favorite_product_ids = [];
if (isLoggedIn()) {
    $favorite_products = getFavoriteProducts($pdo, $_SESSION['user_id']);
    foreach ($favorite_products as $fav) {
        $favorite_product_ids[] = $fav['id'];
    }
}

// Get categories for display
$categories = getCategories($pdo);

// Get some statistics for the homepage
$total_products = count(getAllProducts($pdo));
$total_users_stmt = $pdo->query("SELECT COUNT(*) as count FROM users");
$total_users = $total_users_stmt->fetch(PDO::FETCH_ASSOC)['count'];

// Get active advertisements
$advertisements = getActiveAdvertisements($pdo, 'homepage');

// Function to get CSS class for category
function getCategoryClass($category) {
    $categoryClasses = [
        'Electronics' => 'category-electronics',
        'Clothing' => 'category-clothing',
        'Sports' => 'category-sports',
        'Books' => 'category-books',
        'Home & Garden' => 'category-home-garden'
    ];
    
    return isset($categoryClasses[$category]) ? $categoryClasses[$category] : 'category-electronics';
}
?>

<div class="hero-section">
    <div class="hero-overlay"></div>
    <div class="container hero-content">
        <h1 class="hero-title">مرحبا بك في <span><?php echo SITE_NAME; ?></span></h1>
        <p class="hero-subtitle">متجرك الشامل لجميع احتياجاتك</p>
        <div class="hero-buttons">
            <a href="<?php echo SITE_URL; ?>/products/index.php" class="btn btn-primary btn-large">تسوق الآن</a>
            <a href="#featured-products" class="btn btn-outline btn-large">اكتشف المزيد</a>
        </div>
        <div class="hero-features">
            <div class="feature-item">
                <i class="fas fa-shipping-fast"></i>
                <span>توصيل سريع</span>
            </div>
            <div class="feature-item">
                <i class="fas fa-undo"></i>
                <span>إرجاع مجاني</span>
            </div>
            <div class="feature-item">
                <i class="fas fa-shield-alt"></i>
                <span>ضمان الجودة</span>
            </div>
        </div>
    </div>
</div>

<div class="container">
    <?php if (isset($_SESSION['error_message'])): ?>
        <div class="message error">
            <?php echo htmlspecialchars($_SESSION['error_message']); ?>
            <?php unset($_SESSION['error_message']); ?>
        </div>
    <?php endif; ?>
    
    <?php if (isset($_SESSION['success_message'])): ?>
        <div class="message success">
            <?php echo htmlspecialchars($_SESSION['success_message']); ?>
            <?php unset($_SESSION['success_message']); ?>
        </div>
    <?php endif; ?>
    
    <!-- Statistics Section -->
    <div class="dashboard-stats">
        <div class="stat-card">
            <div class="stat-icon">
                <i class="fas fa-box"></i>
            </div>
            <h3>المنتجات المتاحة</h3>
            <div class="stat-value"><?php echo $total_products; ?></div>
        </div>
        
        <div class="stat-card">
            <div class="stat-icon">
                <i class="fas fa-users"></i>
            </div>
            <h3>اجمالي العملاء</h3>
            <div class="stat-value"><?php echo $total_users; ?></div>
        </div>
        
        <div class="stat-card">
            <div class="stat-icon">
                <i class="fas fa-th-large"></i>
            </div>
            <h3>التصنيفات</h3>
            <div class="stat-value"><?php echo count($categories); ?></div>
        </div>
    </div>
    
    <!-- Categories Section -->
    <div class="section-title">
        <h2>تسوق حسب التصنيف</h2>
        <p class="section-subtitle">استكشف منتجاتنا المتنوعة في كل فئة</p>
    </div>
    
    <div class="categories-grid">
        <?php foreach ($categories as $category): ?>
            <div class="category-card">
                <div class="category-image-container">
                    <?php if (!empty($category['category_image']) && file_exists($category['category_image'])): ?>
                        <img src="<?php echo SITE_URL . '/' . htmlspecialchars($category['category_image']); ?>" alt="<?php echo htmlspecialchars($category['category']); ?>" class="category-image">
                    <?php else: ?>
                        <div class="category-image <?php echo getCategoryClass($category['category']); ?>"></div>
                    <?php endif; ?>
                </div>
                <div class="category-info">
                    <h3><?php echo htmlspecialchars($category['category']); ?></h3>
                    <a href="<?php echo SITE_URL; ?>/products/index.php?category=<?php echo urlencode($category['category']); ?>" class="btn btn-outline btn-block">تصفح <?php echo htmlspecialchars($category['category']); ?></a>
                </div>
            </div>
        <?php endforeach; ?>
    </div>
    
    <!-- Featured Products Section -->
    <div class="section-title" id="featured-products">
        <h2>المنتجات المميزة</h2>
        <p class="section-subtitle">اكتشف أحدث المنتجات المضافة إلى متجرنا</p>
    </div>
    
    <div class="products-grid">
        <?php 
        $all_products = getAllProducts($pdo);
        $featured_products = array_slice($all_products, 0, 4);
        foreach ($featured_products as $product): ?>
            <div class="product-card" data-product-id="<?php echo $product['id']; ?>">
                <div class="product-image">
                    <?php if (!empty($product['image'])): ?>
                        <img src="<?php echo SITE_URL . '/' . htmlspecialchars($product['image']); ?>" alt="<?php echo htmlspecialchars($product['name']); ?>">
                    <?php else: ?>
                        <div class="image-placeholder">
                            <i class="fas fa-image"></i>
                        </div>
                    <?php endif; ?>
                    <button class="favorite-btn <?php echo in_array($product['id'], $favorite_product_ids) ? 'active' : ''; ?>" data-product-id="<?php echo $product['id']; ?>">
                        <i class="fas fa-heart"></i>
                    </button>
                </div>
                <div class="product-info">
                    <h3><?php echo htmlspecialchars($product['name']); ?></h3>
                    <div class="product-price">$<?php echo number_format($product['price'], 2); ?></div>
                    <a href="<?php echo SITE_URL; ?>/products/view.php?id=<?php echo $product['id']; ?>" class="btn btn-primary btn-block">عرض التفاصيل</a>
                </div>
            </div>
        <?php endforeach; ?>
    </div>
    
    <!-- Support Section -->
    <div class="section-title">
        <h2>هل تحتاج مساعدة؟</h2>
        <p class="section-subtitle">نحن هنا للمساعدة في أي استفسار أو مشكلة</p>
    </div>
    
    <div class="support-grid">
        <div class="support-card">
            <div class="support-icon">
                <i class="fas fa-headset"></i>
            </div>
            <div class="support-info">
                <h3>دعم العملاء</h3>
                <p>احصل على مساعدة في طلباتك، الإرجاع، والمزيد</p>
                <?php if (isLoggedIn()): ?>
                    <a href="<?php echo SITE_URL; ?>/tickets/create.php" class="btn btn-outline btn-block">إنشاء تذكرة دعم</a>
                <?php else: ?>
                    <a href="<?php echo SITE_URL; ?>/auth/login.php" class="btn btn-outline btn-block">تسجيل الدخول للحصول على الدعم</a>
                <?php endif; ?>
            </div>
        </div>
        
        <div class="support-card">
            <div class="support-icon">
                <i class="fas fa-truck"></i>
            </div>
            <div class="support-info">
                <h3>معلومات الشحن</h3>
                <p>تعرف على خيارات الشحن وأوقات التسليم</p>
                <a href="<?php echo SITE_URL; ?>/shipping-info.php" class="btn btn-outline btn-block">عرض سياسة الشحن</a>
            </div>
        </div>
        
        <div class="support-card">
            <div class="support-icon">
                <i class="fas fa-undo"></i>
            </div>
            <div class="support-info">
                <h3>الإرجاع والاستبدال</h3>
                <p>إرجاع واستبدال سهل لراحتك البالغة</p>
                <a href="<?php echo SITE_URL; ?>/returns-policy.php" class="btn btn-outline btn-block">عرض سياسة الإرجاع</a>
            </div>
        </div>
        
        <div class="support-card">
            <div class="support-icon">
                <i class="fas fa-question-circle"></i>
            </div>
            <div class="support-info">
                <h3>الأسئلة الشائعة</h3>
                <p>الأسئلة الشائعة والإجابات</p>
                <a href="<?php echo SITE_URL; ?>/faq.php" class="btn btn-outline btn-block">تصفح الأسئلة الشائعة</a>
            </div>
        </div>
    </div>
</div>

<style>
/* Enhanced Hero Section */
.hero-section {
    position: relative;
    background: linear-gradient(rgba(0, 0, 0, 0.7), rgba(0, 0, 0, 0.7)), url('https://images.unsplash.com/photo-1607082350899-7e105aa886ae?ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D&auto=format&fit=crop&w=1920&q=80');
    background-size: cover;
    background-position: center;
    color: white;
    text-align: center;
    padding: 150px 20px;
    margin-bottom: 40px;
    border-radius: 0;
    direction: rtl;
    overflow: hidden;
}

.hero-overlay {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: linear-gradient(135deg, rgba(52, 152, 219, 0.85), rgba(44, 62, 80, 0.9));
    z-index: 1;
}

.hero-content {
    position: relative;
    z-index: 2;
    max-width: 800px;
    margin: 0 auto;
}

.hero-title {
    font-size: 3rem;
    margin-bottom: 20px;
    font-weight: 700;
    text-shadow: 0 2px 4px rgba(0, 0, 0, 0.3);
    animation: fadeInDown 1s ease;
    font-family: 'Tajawal', 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
}

.hero-title span {
    color: #f39c12;
}

.hero-subtitle {
    font-size: 1.3rem;
    margin-bottom: 40px;
    max-width: 700px;
    margin-left: auto;
    margin-right: auto;
    text-shadow: 0 1px 2px rgba(0, 0, 0, 0.3);
    animation: fadeInUp 1s ease 0.3s both;
}

.hero-buttons {
    display: flex;
    justify-content: center;
    gap: 20px;
    flex-wrap: wrap;
    margin-bottom: 40px;
    animation: fadeInUp 1s ease 0.6s both;
}

.hero-buttons .btn {
    margin: 0;
    box-shadow: 0 4px 15px rgba(0, 0, 0, 0.2);
    transition: all 0.3s ease;
    position: relative;
    overflow: hidden;
    font-weight: 600;
    padding: 12px 30px;
    font-size: 1.1rem;
}

.hero-buttons .btn:hover {
    transform: translateY(-3px);
    box-shadow: 0 6px 20px rgba(0, 0, 0, 0.3);
}

.hero-buttons .btn::before {
    content: '';
    position: absolute;
    top: 0;
    left: -100%;
    width: 100%;
    height: 100%;
    background: linear-gradient(90deg, transparent, rgba(255, 255, 255, 0.2), transparent);
    transition: 0.5s;
}

.hero-buttons .btn:hover::before {
    left: 100%;
}

.hero-features {
    display: flex;
    justify-content: center;
    gap: 40px;
    flex-wrap: wrap;
    animation: fadeInUp 1s ease 0.9s both;
}

.feature-item {
    display: flex;
    align-items: center;
    gap: 10px;
    font-size: 1.1rem;
    font-weight: 500;
}

.feature-item i {
    color: #f39c12;
    font-size: 1.4rem;
}

/* Enhanced Statistics Section */
.stat-card {
    background: linear-gradient(135deg, #ffffff 0%, #f8f9fa 100%);
    padding: 25px;
    border-radius: 12px;
    box-shadow: 0 4px 20px rgba(0, 0, 0, 0.08);
    text-align: center;
    direction: rtl;
    transition: all 0.3s ease;
    border: 1px solid #e9ecef;
}

.stat-card:hover {
    transform: translateY(-5px);
    box-shadow: 0 10px 25px rgba(0, 0, 0, 0.15);
}

.stat-icon {
    font-size: 2.5rem;
    margin-bottom: 15px;
    color: #3498db;
}

.stat-card h3 {
    font-size: 1.2rem;
    margin-bottom: 10px;
    color: #2c3e50;
}

.stat-card .stat-value {
    font-size: 2rem;
    font-weight: 700;
    color: #3498db;
}

/* Enhanced Categories Section */
.categories-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(250px, 1fr));
    gap: 30px;
    margin-bottom: 40px;
    direction: rtl;
}

.category-card {
    background-color: #fff;
    border-radius: 12px;
    overflow: hidden;
    box-shadow: 0 4px 15px rgba(0, 0, 0, 0.1);
    transition: all 0.3s ease;
    position: relative;
    display: flex;
    flex-direction: column;
    height: 100%;
    text-align: right;
}

.category-card:hover {
    transform: translateY(-8px);
    box-shadow: 0 12px 25px rgba(0, 0, 0, 0.15);
}

.category-image-container {
    height: 180px;
    width: 100%;
    background-size: cover;
    background-position: center;
    display: flex;
    align-items: center;
    justify-content: center;
    overflow: hidden;
    position: relative;
    border-bottom: 1px solid #eee;
    direction: rtl;
}

.category-image {
    height: 100%;
    width: 100%;
    object-fit: cover;
}

.category-info {
    padding: 20px;
    flex-grow: 1;
    display: flex;
    flex-direction: column;
    text-align: right;
}

.category-info h3 {
    margin-bottom: 15px;
    font-size: 1.3rem;
    color: #2c3e50;
    font-family: 'Tajawal', 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
}

/* Enhanced Support Section */
.support-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(250px, 1fr));
    gap: 30px;
    margin-bottom: 40px;
    direction: rtl;
}

.support-card {
    background: linear-gradient(135deg, #ffffff 0%, #f8f9fa 100%);
    border-radius: 12px;
    overflow: hidden;
    box-shadow: 0 4px 20px rgba(0, 0, 0, 0.08);
    transition: all 0.3s ease;
    position: relative;
    display: flex;
    flex-direction: column;
    height: 100%;
    text-align: right;
    border: 1px solid #e9ecef;
}

.support-card:hover {
    transform: translateY(-5px);
    box-shadow: 0 10px 25px rgba(0, 0, 0, 0.15);
}

.support-icon {
    font-size: 3rem;
    margin: 20px auto 15px;
    color: #3498db;
    text-align: center;
}

.support-info {
    padding: 0 20px 20px;
    flex-grow: 1;
    display: flex;
    flex-direction: column;
    text-align: right;
}

.support-info h3 {
    margin-bottom: 10px;
    font-size: 1.3rem;
    color: #2c3e50;
    font-family: 'Tajawal', 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
    text-align: center;
}

.support-info p {
    color: #7f8c8d;
    margin-bottom: 20px;
    font-size: 0.95rem;
    flex-grow: 1;
    text-align: center;
}

/* Section Titles Enhancement */
.section-title {
    text-align: center;
    margin-bottom: 30px;
    position: relative;
}

.section-title h2 {
    font-size: 2.2rem;
    color: #2c3e50;
    margin-bottom: 10px;
    font-family: 'Tajawal', 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
}

.section-subtitle {
    font-size: 1.1rem;
    color: #7f8c8d;
    margin-top: 10px;
    font-style: italic;
}

.section-title h2::after {
    content: '';
    display: block;
    width: 70px;
    height: 4px;
    background: linear-gradient(to left, #3498db, #2c3e50);
    margin: 15px auto;
    border-radius: 2px;
}

/* Offer Placeholder */
.offer-placeholder {
    width: 100%;
    height: 100%;
    display: flex;
    align-items: center;
    justify-content: center;
    background: linear-gradient(135deg, #f5f7fa 0%, #e4e7f4 100%);
}

.offer-placeholder i {
    font-size: 3rem;
    color: #f39c12;
}

/* Animated Offers Styles */
.animated-offers {
    position: relative;
    overflow: hidden;
}

.offer-card {
    animation: pulse 2s infinite;
    transition: transform 0.3s ease, box-shadow 0.3s ease;
}

.offer-card:hover {
    transform: translateY(-5px);
    box-shadow: 0 10px 20px rgba(0, 0, 0, 0.15);
}

@keyframes pulse {
    0% {
        box-shadow: 0 0 0 0 rgba(52, 152, 219, 0.4);
    }
    70% {
        box-shadow: 0 0 0 10px rgba(52, 152, 219, 0);
    }
    100% {
        box-shadow: 0 0 0 0 rgba(52, 152, 219, 0);
    }
}

/* Additional animation for the section */
.section-title h2 {
    position: relative;
    display: inline-block;
}

.section-title h2::after {
    content: '';
    position: absolute;
    bottom: -10px;
    left: 50%;
    transform: translateX(-50%);
    width: 0;
    height: 3px;
    background: linear-gradient(to right, #3498db, #2c3e50);
    transition: width 0.5s ease;
}

.section-title h2:hover::after {
    width: 100px;
}

/* Responsive adjustments */
@media (max-width: 768px) {
    .hero-section {
        padding: 100px 20px;
    }
    
    .hero-title {
        font-size: 2.2rem;
    }
    
    .hero-subtitle {
        font-size: 1.1rem;
    }
    
    .hero-buttons {
        flex-direction: column;
        gap: 15px;
    }
    
    .hero-features {
        gap: 20px;
    }
    
    .categories-grid,
    .support-grid {
        grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
        gap: 20px;
    }
    
    .category-image {
        height: 150px;
    }
}

@media (max-width: 480px) {
    .hero-title {
        font-size: 1.8rem;
    }
    
    .hero-subtitle {
        font-size: 1rem;
    }
    
    .categories-grid,
    .support-grid {
        grid-template-columns: 1fr;
    }
    
    .stat-card .stat-value {
        font-size: 1.5rem;
    }
}
</style>

<script>
// Add additional animation effects
document.addEventListener('DOMContentLoaded', function() {
    const offerCards = document.querySelectorAll('.offer-card');
    
    // Add staggered animation delay
    offerCards.forEach((card, index) => {
        card.style.animationDelay = (index * 0.2) + 's';
    });
    
    // Add entrance animation
    const observer = new IntersectionObserver((entries) => {
        entries.forEach(entry => {
            if (entry.isIntersecting) {
                entry.target.style.opacity = 1;
                entry.target.style.transform = 'translateY(0)';
            }
        });
    }, { threshold: 0.1 });
    
    // Apply to animated offers section
    const animatedOffers = document.querySelector('.animated-offers');
    if (animatedOffers) {
        animatedOffers.style.opacity = 0;
        animatedOffers.style.transform = 'translateY(20px)';
        animatedOffers.style.transition = 'opacity 0.6s ease, transform 0.6s ease';
        observer.observe(animatedOffers);
    }
    
    // Add animation to category cards
    const categoryCards = document.querySelectorAll('.category-card');
    categoryCards.forEach((card, index) => {
        card.style.opacity = 0;
        card.style.transform = 'translateY(20px)';
        card.style.transition = 'opacity 0.6s ease, transform 0.6s ease';
        card.style.transitionDelay = (index * 0.1) + 's';
        
        const cardObserver = new IntersectionObserver((entries) => {
            entries.forEach(entry => {
                if (entry.isIntersecting) {
                    entry.target.style.opacity = 1;
                    entry.target.style.transform = 'translateY(0)';
                }
            });
        }, { threshold: 0.1 });
        
        cardObserver.observe(card);
    });
    
    // Add animation to support cards
    const supportCards = document.querySelectorAll('.support-card');
    supportCards.forEach((card, index) => {
        card.style.opacity = 0;
        card.style.transform = 'translateY(20px)';
        card.style.transition = 'opacity 0.6s ease, transform 0.6s ease';
        card.style.transitionDelay = (index * 0.1) + 's';
        
        const cardObserver = new IntersectionObserver((entries) => {
            entries.forEach(entry => {
                if (entry.isIntersecting) {
                    entry.target.style.opacity = 1;
                    entry.target.style.transform = 'translateY(0)';
                }
            });
        }, { threshold: 0.1 });
        
        cardObserver.observe(card);
    });
});
</script>

<?php require_once 'includes/footer.php'; ?>