<?php
// ===================================
// ARCHIVO: index.php
// Landing page principal de Luna Mayorista
// ===================================

$page_title = "Inicio";
$page_description = "Plataforma B2B de calzado mayorista argentino - Acceso directo a fábricas";
$breadcrumbs = [];
$additional_css = ['assets/css/home.css'];

require_once 'config/session.php';
debugSession('Home');

require_once 'config/database.php';
require_once 'includes/functions.php';

// Verificar token "recordarme" si no está logueado
if (!isLoggedIn()) {
    checkRememberToken();
}

// Obtener estadísticas para mostrar en la landing
try {
    // Contar productos activos
    $products_stmt = $pdo->query("SELECT COUNT(*) as total FROM products WHERE is_active = TRUE");
    $total_products = $products_stmt->fetch()['total'];

    // Contar fábricas activas
    $factories_stmt = $pdo->query("SELECT COUNT(*) as total FROM factories WHERE is_active = TRUE");
    $total_factories = $factories_stmt->fetch()['total'];

    // Obtener algunos productos destacados para preview
    $featured_stmt = $pdo->query("
        SELECT p.*, f.name as factory_name,
               (SELECT pi.image_url FROM product_images pi 
                WHERE pi.product_id = p.id AND pi.image_type = 'main' 
                LIMIT 1) as main_image
        FROM products p
        LEFT JOIN factories f ON p.factory_id = f.id
        WHERE p.is_active = TRUE
        ORDER BY p.created_at DESC
        LIMIT 6
    ");
    $featured_products = $featured_stmt->fetchAll(PDO::FETCH_ASSOC);

} catch (PDOException $e) {
    error_log("Home page stats error: " . $e->getMessage());
    $total_products = 0;
    $total_factories = 0;
    $featured_products = [];
}

require_once 'includes/header.php';
?>

<main class="home-main">
    <!-- Hero Section -->
    <section class="hero-section">
        <div class="container">
            <div class="hero-content">
                <div class="hero-text">
                    <h1 class="hero-title">
                        🏭 Plataforma B2B de<br>
                        <span class="highlight">Calzado Mayorista</span>
                    </h1>
                    <p class="hero-subtitle">
                        Acceso directo a fábricas exclusivas de calzado argentino.<br>
                        Precios mayoristas reales, sin intermediarios.
                    </p>
                    
                    <div class="hero-stats">
                        <div class="stat-item">
                            <span class="stat-number"><?php echo number_format($total_products); ?></span>
                            <span class="stat-label">Productos</span>
                        </div>
                        <div class="stat-item">
                            <span class="stat-number"><?php echo $total_factories; ?>+</span>
                            <span class="stat-label">Fábricas</span>
                        </div>
                        <div class="stat-item">
                            <span class="stat-number">48h</span>
                            <span class="stat-label">Aprobación</span>
                        </div>
                    </div>

                    <div class="hero-actions">
                        <?php if (!isLoggedIn()): ?>
                            <a href="register.php" class="btn btn-primary btn-large">
                                🚀 Registrar Comercio
                            </a>
                            <a href="catalog.php" class="btn btn-secondary btn-large">
                                👁️ Ver Catálogo Público
                            </a>
                        <?php elseif (!isApproved()): ?>
                            <a href="pending-approval.php" class="btn btn-warning btn-large">
                                ⏳ Ver Estado de Aprobación
                            </a>
                            <a href="catalog.php" class="btn btn-secondary btn-large">
                                👁️ Catálogo Público
                            </a>
                        <?php else: ?>
                            <a href="catalog.php" class="btn btn-primary btn-large">
                                🛍️ Acceder al Catálogo Mayorista
                            </a>
                        <?php endif; ?>
                    </div>
                </div>

                <div class="hero-image">
                    <div class="hero-visual">
                        <div class="floating-card card-1">
                            <span class="card-icon">👟</span>
                            <span class="card-text">Calzado de calidad</span>
                        </div>
                        <div class="floating-card card-2">
                            <span class="card-icon">🏭</span>
                            <span class="card-text">Directo de fábrica</span>
                        </div>
                        <div class="floating-card card-3">
                            <span class="card-icon">💰</span>
                            <span class="card-text">Precios mayoristas</span>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </section>

    <!-- Preview del Catálogo -->
    <?php if (!empty($featured_products)): ?>
    <section class="catalog-preview">
        <div class="container">
            <div class="section-header">
                <h2>Vista previa del catálogo</h2>
                <p>Algunos de nuestros productos más recientes</p>
                <?php if (!isLoggedIn()): ?>
                    <div class="access-notice">
                        <span class="notice-icon">🔒</span>
                        <span>Precios disponibles solo para comercios registrados</span>
                    </div>
                <?php endif; ?>
            </div>

            <div class="products-preview-grid">
                <?php foreach ($featured_products as $product): ?>
                    <div class="preview-product-card">
                        <div class="preview-product-image">
                            <?php if ($product['main_image']): ?>
                                <img src="<?php echo htmlspecialchars($product['main_image']); ?>" 
                                     alt="<?php echo htmlspecialchars($product['name']); ?>"
                                     loading="lazy">
                            <?php else: ?>
                                <div class="no-image">
                                    <span>📷</span>
                                </div>
                            <?php endif; ?>
                            
                            <div class="preview-badges">
                                <span class="badge badge-<?php echo $product['gender']; ?>">
                                    <?php echo ucfirst($product['gender']); ?>
                                </span>
                            </div>
                        </div>
                        
                        <div class="preview-product-info">
                            <h3><?php echo htmlspecialchars($product['name']); ?></h3>
                            <p class="preview-factory">🏭 <?php echo htmlspecialchars($product['factory_name']); ?></p>
                            
                            <div class="preview-pricing">
                                <?php if (isLoggedIn() && isApproved()): ?>
                                    <span class="price"><?php echo formatPrice($product['wholesale_price']); ?></span>
                                <?php else: ?>
                                    <span class="price-hidden">Precio para mayoristas</span>
                                <?php endif; ?>
                            </div>
                        </div>
                    </div>
                <?php endforeach; ?>
            </div>

            <div class="preview-footer">
                <a href="catalog.php" class="btn btn-primary">
                    Ver catálogo completo
                </a>
            </div>
        </div>
    </section>
    <?php endif; ?>

    <!-- Características -->
    <section class="features-section">
        <div class="container">
            <h2>¿Por qué elegir Luna Mayorista?</h2>
            
            <div class="features-grid">
                <div class="feature-card">
                    <div class="feature-icon">🏭</div>
                    <h3>Acceso Directo a Fábricas</h3>
                    <p>Trabajamos con fábricas que normalmente no aceptan clientes nuevos. Sin intermediarios.</p>
                </div>
                
                <div class="feature-card">
                    <div class="feature-icon">💰</div>
                    <h3>Precios Mayoristas Reales</h3>
                    <p>Precios de fábrica exclusivos para comercios verificados. Márgenes competitivos garantizados.</p>
                </div>
                
                <div class="feature-card">
                    <div class="feature-icon">👥</div>
                    <h3>Proceso de Verificación</h3>
                    <p>Validamos cada comercio personalmente para mantener la exclusividad y calidad del servicio.</p>
                </div>
                
                <div class="feature-card">
                    <div class="feature-icon">📦</div>
                    <h3>Sistema de Tareas</h3>
                    <p>Venta por tareas (6 o 12 pares) con distribución de talles optimizada por género.</p>
                </div>
                
                <div class="feature-card">
                    <div class="feature-icon">⚡</div>
                    <h3>Aprobación Rápida</h3>
                    <p>Proceso de verificación en 24-48 horas hábiles. Soporte telefónico personalizado.</p>
                </div>
                
                <div class="feature-card">
                    <div class="feature-icon">🔒</div>
                    <h3>Plataforma Segura</h3>
                    <p>Información comercial protegida. Acceso controlado solo para usuarios aprobados.</p>
                </div>
            </div>
        </div>
    </section>

    <!-- Proceso de Registro -->
    <section class="process-section">
        <div class="container">
            <h2>Cómo funciona</h2>
            
            <div class="process-steps">
                <div class="process-step">
                    <div class="step-number">1</div>
                    <div class="step-content">
                        <h3>Registro</h3>
                        <p>Complete el formulario con los datos de su comercio y CUIT</p>
                    </div>
                </div>
                
                <div class="process-arrow">→</div>
                
                <div class="process-step">
                    <div class="step-number">2</div>
                    <div class="step-content">
                        <h3>Verificación</h3>
                        <p>Nuestro equipo revisa la información y lo contacta telefónicamente</p>
                    </div>
                </div>
                
                <div class="process-arrow">→</div>
                
                <div class="process-step">
                    <div class="step-number">3</div>
                    <div class="step-content">
                        <h3>Aprobación</h3>
                        <p>Una vez verificado, accede a precios mayoristas y funciones completas</p>
                    </div>
                </div>
                
                <div class="process-arrow">→</div>
                
                <div class="process-step">
                    <div class="step-number">4</div>
                    <div class="step-content">
                        <h3>Compra</h3>
                        <p>Arme su pedido online, nosotros coordinamos con la fábrica</p>
                    </div>
                </div>
            </div>
        </div>
    </section>

    <!-- Call to Action -->
    <section class="cta-section">
        <div class="container">
            <div class="cta-content">
                <h2>¿Listo para acceder a precios de fábrica?</h2>
                <p>Únase a cientos de comercios que ya confían en nuestra plataforma</p>
                
                <?php if (!isLoggedIn()): ?>
                    <div class="cta-actions">
                        <a href="register.php" class="btn btn-primary btn-large">
                            Registrar mi comercio
                        </a>
                        <div class="cta-note">
                            <small>✓ Gratis • ✓ Aprobación en 48hs • ✓ Sin comisiones</small>
                        </div>
                    </div>
                <?php elseif (!isApproved()): ?>
                    <div class="cta-actions">
                        <a href="pending-approval.php" class="btn btn-warning btn-large">
                            Ver estado de mi solicitud
                        </a>
                    </div>
                <?php else: ?>
                    <div class="cta-actions">
                        <a href="catalog.php" class="btn btn-primary btn-large">
                            Explorar catálogo mayorista
                        </a>
                    </div>
                <?php endif; ?>
            </div>
        </div>
    </section>
</main>

<style>
/* Estilos específicos para la home page */
.hero-section {
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    color: white;
    padding: 4rem 0;
    position: relative;
    overflow: hidden;
}

.hero-content {
    display: grid;
    grid-template-columns: 1fr 1fr;
    align-items: center;
    gap: 3rem;
    min-height: 500px;
}

.hero-title {
    font-size: 3rem;
    font-weight: 800;
    line-height: 1.2;
    margin-bottom: 1.5rem;
}

.highlight {
    color: #ffd700;
}

.hero-subtitle {
    font-size: 1.2rem;
    margin-bottom: 2rem;
    opacity: 0.9;
    line-height: 1.6;
}

.hero-stats {
    display: flex;
    gap: 2rem;
    margin-bottom: 2rem;
}

.stat-item {
    text-align: center;
}

.stat-number {
    display: block;
    font-size: 2rem;
    font-weight: 700;
    color: #ffd700;
}

.stat-label {
    font-size: 0.9rem;
    opacity: 0.8;
}

.hero-actions {
    display: flex;
    gap: 1rem;
    flex-wrap: wrap;
}

.btn-large {
    padding: 1rem 2rem;
    font-size: 1.1rem;
    font-weight: 600;
}

.hero-visual {
    position: relative;
    height: 400px;
}

.floating-card {
    position: absolute;
    background: rgba(255, 255, 255, 0.15);
    backdrop-filter: blur(10px);
    border: 1px solid rgba(255, 255, 255, 0.2);
    border-radius: 12px;
    padding: 1rem 1.5rem;
    display: flex;
    align-items: center;
    gap: 0.5rem;
    animation: float 6s ease-in-out infinite;
}

.floating-card.card-1 {
    top: 20%;
    left: 10%;
    animation-delay: 0s;
}

.floating-card.card-2 {
    top: 50%;
    right: 10%;
    animation-delay: 2s;
}

.floating-card.card-3 {
    bottom: 20%;
    left: 20%;
    animation-delay: 4s;
}

@keyframes float {
    0%, 100% { transform: translateY(0px); }
    50% { transform: translateY(-20px); }
}

.catalog-preview {
    padding: 4rem 0;
    background: #f8f9fa;
}

.section-header {
    text-align: center;
    margin-bottom: 3rem;
}

.section-header h2 {
    font-size: 2.5rem;
    margin-bottom: 1rem;
    color: #2c3e50;
}

.access-notice {
    display: inline-flex;
    align-items: center;
    gap: 0.5rem;
    background: #fff3cd;
    color: #856404;
    padding: 0.5rem 1rem;
    border-radius: 20px;
    font-size: 0.9rem;
    margin-top: 1rem;
}

.products-preview-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
    gap: 2rem;
    margin-bottom: 3rem;
}

.preview-product-card {
    background: white;
    border-radius: 12px;
    overflow: hidden;
    box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
    transition: transform 0.3s ease, box-shadow 0.3s ease;
}

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

.preview-product-image {
    position: relative;
    height: 200px;
    overflow: hidden;
}

.preview-product-image img {
    width: 100%;
    height: 100%;
    object-fit: cover;
}

.no-image {
    display: flex;
    align-items: center;
    justify-content: center;
    height: 100%;
    background: #f1f3f4;
    font-size: 3rem;
}

.preview-badges {
    position: absolute;
    top: 0.5rem;
    left: 0.5rem;
}

.badge {
    padding: 0.25rem 0.5rem;
    border-radius: 4px;
    font-size: 0.75rem;
    font-weight: 600;
    color: white;
}

.badge-hombre { background: #3498db; }
.badge-mujer { background: #e91e63; }
.badge-unisex { background: #9c27b0; }
.badge-niño { background: #4caf50; }
.badge-niña { background: #ff5722; }

.preview-product-info {
    padding: 1.5rem;
}

.preview-product-info h3 {
    margin-bottom: 0.5rem;
    color: #2c3e50;
}

.preview-factory {
    color: #7f8c8d;
    font-size: 0.9rem;
    margin-bottom: 1rem;
}

.price {
    font-size: 1.2rem;
    font-weight: 700;
    color: #27ae60;
}

.price-hidden {
    color: #95a5a6;
    font-style: italic;
}

.preview-footer {
    text-align: center;
}

.features-section {
    padding: 4rem 0;
}

.features-section h2 {
    text-align: center;
    font-size: 2.5rem;
    margin-bottom: 3rem;
    color: #2c3e50;
}

.features-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
    gap: 2rem;
}

.feature-card {
    text-align: center;
    padding: 2rem;
    background: white;
    border-radius: 12px;
    box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
    transition: transform 0.3s ease;
}

.feature-card:hover {
    transform: translateY(-5px);
}

.feature-icon {
    font-size: 3rem;
    margin-bottom: 1rem;
}

.feature-card h3 {
    color: #2c3e50;
    margin-bottom: 1rem;
}

.process-section {
    padding: 4rem 0;
    background: #f8f9fa;
}

.process-section h2 {
    text-align: center;
    font-size: 2.5rem;
    margin-bottom: 3rem;
    color: #2c3e50;
}

.process-steps {
    display: flex;
    align-items: center;
    justify-content: center;
    flex-wrap: wrap;
    gap: 1rem;
}

.process-step {
    text-align: center;
    max-width: 200px;
}

.step-number {
    width: 60px;
    height: 60px;
    background: #3498db;
    color: white;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 1.5rem;
    font-weight: 700;
    margin: 0 auto 1rem auto;
}

.process-arrow {
    font-size: 2rem;
    color: #3498db;
    margin: 0 1rem;
}

.cta-section {
    padding: 4rem 0;
    background: linear-gradient(135deg, #2c3e50 0%, #34495e 100%);
    color: white;
    text-align: center;
}

.cta-content h2 {
    font-size: 2.5rem;
    margin-bottom: 1rem;
}

.cta-content p {
    font-size: 1.2rem;
    margin-bottom: 2rem;
    opacity: 0.9;
}

.cta-note {
    margin-top: 1rem;
}

.cta-note small {
    opacity: 0.8;
}

/* Responsive */
@media (max-width: 768px) {
    .hero-content {
        grid-template-columns: 1fr;
        text-align: center;
    }
    
    .hero-title {
        font-size: 2rem;
    }
    
    .hero-stats {
        justify-content: center;
    }
    
    .products-preview-grid {
        grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
    }
    
    .process-steps {
        flex-direction: column;
    }
    
    .process-arrow {
        transform: rotate(90deg);
    }
}
</style>

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