<!DOCTYPE html>
<html lang="fr">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>{{ $product->name }} - 
        @if($user->role === 'seller' && $user->shop_name)
            {{ $user->shop_name }}
        @else
            Boutique de {{ $user->name }}
        @endif
    </title>
    <link rel="icon" type="image/svg+xml" href="{{ asset('favicon.svg') }}">
    <link rel="icon" type="image/x-icon" href="{{ asset('favicon.ico') }}">

    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.1/css/all.min.css">
    <link href="https://unpkg.com/aos@2.3.1/dist/aos.css" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css2?family=Cormorant+Garamond:wght@400;600;700&family=Montserrat:wght@300;400;500;600;700&display=swap" rel="stylesheet">

    <style>
        :root {
            --primary: #10b981;
            --primary-dark: #059669;
            --accent: #10b981;
            --gradient-1: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            --gradient-2: linear-gradient(135deg, #10b981 0%, #059669 100%);
        }

        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: 'Montserrat', sans-serif;
            color: #1e293b;
            background: #f8fafc;
        }

        h1, h2, h3, h4, h5, h6 {
            font-family: 'Cormorant Garamond', serif;
        }

        /* TOP BAR */
        .top-bar {
            background: linear-gradient(135deg, #0f172a, #1e293b);
            color: white;
            padding: 10px 0;
            font-size: 0.85rem;
        }

        .top-bar a {
            color: white;
            text-decoration: none;
            transition: color 0.3s;
        }

        .top-bar a:hover {
            color: var(--accent);
        }

        /* NAVIGATION */
        .main-nav {
            background: white;
            box-shadow: 0 2px 20px rgba(0,0,0,0.1);
            padding: 20px 0;
            position: sticky;
            top: 0;
            z-index: 1000;
        }

        .brand-logo {
            display: flex;
            align-items: center;
            gap: 12px;
            font-size: 1.5rem;
            font-weight: 700;
            color: #1e293b;
            text-decoration: none;
            font-family: 'Cormorant Garamond', serif;
        }

        .brand-logo i {
            font-size: 2rem;
            color: var(--accent);
        }

        .nav-links {
            list-style: none;
            display: flex;
            gap: 40px;
            margin: 0;
        }

        .nav-links a {
            color: #475569;
            text-decoration: none;
            font-weight: 500;
            transition: color 0.3s;
        }

        .nav-links a:hover {
            color: var(--accent);
        }

        .cart-btn {
            display: flex;
            align-items: center;
            gap: 8px;
            background: var(--gradient-2);
            color: white;
            padding: 10px 20px;
            border-radius: 50px;
            text-decoration: none;
            font-weight: 600;
            transition: all 0.3s;
            position: relative;
        }

        .cart-btn:hover {
            transform: translateY(-2px);
            box-shadow: 0 5px 15px rgba(16, 185, 129, 0.4);
            color: white;
        }

        .cart-badge {
            position: absolute;
            top: -5px;
            right: -5px;
            background: #ef4444;
            color: white;
            width: 22px;
            height: 22px;
            border-radius: 50%;
            display: flex;
            align-items: center;
            justify-content: center;
            font-size: 0.75rem;
        }

        /* PRODUCT DETAIL */
        .product-detail-section {
            padding: 80px 0;
        }

        .breadcrumb-custom {
            background: transparent;
            padding: 20px 0;
            margin-bottom: 40px;
        }

        .breadcrumb-custom a {
            color: var(--accent);
            text-decoration: none;
        }

        .product-image-main {
            width: 100%;
            height: 500px;
            background: white;
            border-radius: 20px;
            overflow: hidden;
            box-shadow: 0 10px 40px rgba(0,0,0,0.1);
            display: flex;
            align-items: center;
            justify-content: center;
        }

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

        .product-info-card {
            background: white;
            padding: 40px;
            border-radius: 20px;
            box-shadow: 0 10px 40px rgba(0,0,0,0.1);
        }

        .product-category-badge {
            display: inline-block;
            background: rgba(16, 185, 129, 0.1);
            color: var(--accent);
            padding: 8px 20px;
            border-radius: 50px;
            font-size: 0.85rem;
            font-weight: 600;
            text-transform: uppercase;
            letter-spacing: 1px;
            margin-bottom: 20px;
        }

        .product-title {
            font-size: 2.5rem;
            font-weight: 700;
            margin-bottom: 20px;
            color: #0f172a;
        }

        .product-price {
            font-size: 2.5rem;
            font-weight: 700;
            color: var(--accent);
            margin-bottom: 30px;
        }

        .product-price small {
            font-size: 1.2rem;
            color: #64748b;
        }

        .stock-status {
            display: inline-flex;
            align-items: center;
            gap: 8px;
            padding: 10px 20px;
            border-radius: 50px;
            font-weight: 600;
            margin-bottom: 30px;
        }

        .stock-status.in-stock {
            background: rgba(16, 185, 129, 0.1);
            color: #059669;
        }

        .stock-status.low-stock {
            background: rgba(251, 191, 36, 0.1);
            color: #f59e0b;
        }

        .stock-status.out-stock {
            background: rgba(239, 68, 68, 0.1);
            color: #dc2626;
        }

        .product-description {
            line-height: 1.8;
            color: #475569;
            margin-bottom: 30px;
            font-size: 1.05rem;
        }

        .quantity-selector {
            display: flex;
            align-items: center;
            gap: 15px;
            margin-bottom: 30px;
        }

        .quantity-selector label {
            font-weight: 600;
            color: #1e293b;
        }

        .quantity-controls {
            display: flex;
            align-items: center;
            gap: 10px;
            background: #f1f5f9;
            padding: 8px 15px;
            border-radius: 50px;
        }

        .quantity-controls button {
            width: 35px;
            height: 35px;
            border: none;
            background: white;
            color: var(--accent);
            border-radius: 50%;
            font-size: 1.2rem;
            cursor: pointer;
            transition: all 0.3s;
        }

        .quantity-controls button:hover {
            background: var(--accent);
            color: white;
            transform: scale(1.1);
        }

        .quantity-controls input {
            width: 60px;
            text-align: center;
            border: none;
            background: transparent;
            font-weight: 600;
            font-size: 1.1rem;
        }

        .add-to-cart-btn {
            width: 100%;
            padding: 18px;
            background: var(--gradient-2);
            color: white;
            border: none;
            border-radius: 50px;
            font-size: 1.1rem;
            font-weight: 700;
            cursor: pointer;
            transition: all 0.3s;
            display: flex;
            align-items: center;
            justify-content: center;
            gap: 10px;
        }

        .add-to-cart-btn:hover {
            transform: translateY(-3px);
            box-shadow: 0 10px 30px rgba(16, 185, 129, 0.4);
        }

        .add-to-cart-btn:disabled {
            opacity: 0.6;
            cursor: not-allowed;
        }

        .product-features {
            display: grid;
            grid-template-columns: repeat(2, 1fr);
            gap: 20px;
            margin-top: 40px;
            padding-top: 40px;
            border-top: 2px solid #f1f5f9;
        }

        .feature-item {
            display: flex;
            align-items: center;
            gap: 12px;
        }

        .feature-icon {
            width: 50px;
            height: 50px;
            background: rgba(16, 185, 129, 0.1);
            border-radius: 50%;
            display: flex;
            align-items: center;
            justify-content: center;
            color: var(--accent);
            font-size: 1.3rem;
        }

        .feature-text {
            flex: 1;
        }

        .feature-text strong {
            display: block;
            color: #0f172a;
            margin-bottom: 3px;
        }

        .feature-text span {
            color: #64748b;
            font-size: 0.9rem;
        }

        /* FOOTER */
        .footer {
            background: linear-gradient(135deg, #0f172a, #1e293b);
            color: #cbd5e1;
            padding: 60px 0 30px;
            margin-top: 80px;
        }

        .footer h5 {
            color: white;
            font-size: 1.3rem;
            margin-bottom: 20px;
        }

        .footer a {
            color: #94a3b8;
            text-decoration: none;
            transition: all 0.3s;
        }

        .footer a:hover {
            color: var(--accent);
        }

        .footer-bottom {
            text-align: center;
            margin-top: 50px;
            padding-top: 30px;
            border-top: 1px solid rgba(255,255,255,0.1);
            color: #64748b;
        }
    </style>
</head>
<body>

<!-- TOP BAR -->
<div class="top-bar">
    <div class="container">
        <div class="d-flex justify-content-between align-items-center">
            <div>
                <i class="fas fa-phone me-2"></i>
                <a href="tel:{{ $user->phone }}">{{ $user->phone ?? '+225 XX XX XX XX XX' }}</a>
                <span class="mx-3">|</span>
                <i class="fas fa-envelope me-2"></i>
                <a href="mailto:{{ $user->email }}">{{ $user->email ?? 'contact@boutique.com' }}</a>
            </div>
            <div>
                <i class="fas fa-shipping-fast me-2"></i>
                Livraison Gratuite dès 50 000 XOF
            </div>
        </div>
    </div>
</div>

<!-- NAVIGATION -->
<nav class="main-nav">
    <div class="container">
        <div class="d-flex justify-content-between align-items-center">
            <a href="{{ route('user.shop', $user->username ?? $user->member_code ?? $user->id) }}" class="brand-logo">
                <i class="fas fa-gem"></i>
                <span>
                    @if($user->role === 'seller' && $user->shop_name)
                        {{ $user->shop_name }}
                    @else
                        Boutique de {{ $user->name }}
                    @endif
                </span>
            </a>

            <ul class="nav-links d-none d-lg-flex">
                <li><a href="{{ route('user.shop', $user->username ?? $user->member_code ?? $user->id) }}">Accueil</a></li>
                <li><a href="{{ route('user.shop.collection', $user->username ?? $user->member_code ?? $user->id) }}">Collection</a></li>
                <li><a href="{{ route('user.shop.about', $user->username ?? $user->member_code ?? $user->id) }}">À Propos</a></li>
                <li><a href="{{ route('user.shop.contact', $user->username ?? $user->member_code ?? $user->id) }}">Contact</a></li>
            </ul>

            <a href="{{ route('user.shop.cart', $user->username ?? $user->member_code ?? $user->id) }}" class="cart-btn">
                <i class="fas fa-shopping-bag"></i>
                Panier
                <span class="cart-badge">0</span>
            </a>
        </div>
    </div>
</nav>

<!-- BREADCRUMB -->
<div class="container">
    <nav class="breadcrumb-custom">
        <a href="{{ route('user.shop', $user->username ?? $user->member_code ?? $user->id) }}">
            <i class="fas fa-home"></i> Accueil
        </a>
        <span class="mx-2">/</span>
        <a href="{{ route('user.shop.collection', $user->username ?? $user->member_code ?? $user->id) }}">Collection</a>
        <span class="mx-2">/</span>
        <span>{{ $product->name }}</span>
    </nav>
</div>

<!-- PRODUCT DETAIL -->
<section class="product-detail-section">
    <div class="container">
        <div class="row g-4">
            <!-- Image -->
            <div class="col-lg-6" data-aos="fade-right">
                <div class="product-image-main">
                    @if($product->image)
                        <img src="{{ asset('storage/' . $product->image) }}" alt="{{ $product->name }}">
                    @else
                        <i class="fas fa-gem" style="font-size: 8rem; color: rgba(0,0,0,0.1);"></i>
                    @endif
                </div>
            </div>

            <!-- Info -->
            <div class="col-lg-6" data-aos="fade-left">
                <div class="product-info-card">
                    <span class="product-category-badge">
                        {{ $product->category->name ?? 'Bijoux' }}
                    </span>

                    <h1 class="product-title">{{ $product->name }}</h1>

                    <div class="product-price">
                        {{ number_format($product->price, 0, ',', ' ') }} <small>XOF</small>
                    </div>

                    @if($product->stock > 0)
                        @if($product->stock < 10)
                            <div class="stock-status low-stock">
                                <i class="fas fa-fire"></i>
                                <span>Stock limité - Plus que {{ $product->stock }} disponibles</span>
                            </div>
                        @else
                            <div class="stock-status in-stock">
                                <i class="fas fa-check-circle"></i>
                                <span>En stock</span>
                            </div>
                        @endif
                    @else
                        <div class="stock-status out-stock">
                            <i class="fas fa-times-circle"></i>
                            <span>Rupture de stock</span>
                        </div>
                    @endif

                    <p class="product-description">
                        {{ $product->description ?? 'Découvrez ce produit d\'exception, conçu avec soin pour sublimer vos moments précieux.' }}
                    </p>

                    @if($product->stock > 0)
                        <!-- Quantity Selector -->
                        <div class="quantity-selector">
                            <label>Quantité:</label>
                            <div class="quantity-controls">
                                <button type="button" onclick="decrementQuantity()">
                                    <i class="fas fa-minus"></i>
                                </button>
                                <input type="number" id="quantity" value="1" min="1" max="{{ $product->stock }}" readonly>
                                <button type="button" onclick="incrementQuantity()">
                                    <i class="fas fa-plus"></i>
                                </button>
                            </div>
                        </div>

                        <!-- Add to Cart Button -->
                        <button class="add-to-cart-btn" onclick="addToCartDetail()">
                            <i class="fas fa-shopping-bag"></i>
                            <span>Ajouter au Panier</span>
                        </button>
                    @else
                        <button class="add-to-cart-btn" disabled>
                            <i class="fas fa-times-circle"></i>
                            <span>Produit Indisponible</span>
                        </button>
                    @endif

                    <!-- Product Features -->
                    <div class="product-features">
                        <div class="feature-item">
                            <div class="feature-icon">
                                <i class="fas fa-shield-alt"></i>
                            </div>
                            <div class="feature-text">
                                <strong>Qualité Garantie</strong>
                                <span>Produits authentiques</span>
                            </div>
                        </div>

                        <div class="feature-item">
                            <div class="feature-icon">
                                <i class="fas fa-shipping-fast"></i>
                            </div>
                            <div class="feature-text">
                                <strong>Livraison Rapide</strong>
                                <span>Sous 2-5 jours</span>
                            </div>
                        </div>

                        <div class="feature-item">
                            <div class="feature-icon">
                                <i class="fas fa-undo"></i>
                            </div>
                            <div class="feature-text">
                                <strong>Retours Faciles</strong>
                                <span>14 jours pour changer d'avis</span>
                            </div>
                        </div>

                        <div class="feature-item">
                            <div class="feature-icon">
                                <i class="fas fa-lock"></i>
                            </div>
                            <div class="feature-text">
                                <strong>Paiement Sécurisé</strong>
                                <span>Transactions protégées</span>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</section>

<!-- FOOTER -->
<footer class="footer">
    <div class="container">
        <div class="row g-4">
            <div class="col-lg-4">
                <h5>
                    @if($user->role === 'seller' && $user->shop_name)
                        {{ $user->shop_name }}
                    @else
                        Boutique de {{ $user->name }}
                    @endif
                </h5>
                <p style="opacity: 0.8;">Des produits d'exception pour des moments inoubliables.</p>
            </div>

            <div class="col-lg-4">
                <h5>Navigation</h5>
                <div class="d-flex flex-column gap-2">
                    <a href="{{ route('user.shop', $user->username ?? $user->member_code ?? $user->id) }}">Accueil</a>
                    <a href="{{ route('user.shop.collection', $user->username ?? $user->member_code ?? $user->id) }}">Collection</a>
                    <a href="{{ route('user.shop.about', $user->username ?? $user->member_code ?? $user->id) }}">À Propos</a>
                    <a href="{{ route('user.shop.contact', $user->username ?? $user->member_code ?? $user->id) }}">Contact</a>
                </div>
            </div>

            <div class="col-lg-4">
                <h5>Contact</h5>
                <div class="d-flex flex-column gap-2">
                    <span><i class="fas fa-map-marker-alt me-2"></i>{{ $user->city ?? 'Ville' }}, {{ $user->country ?? 'Pays' }}</span>
                    <a href="tel:{{ $user->phone }}"><i class="fas fa-phone me-2"></i>{{ $user->phone ?? 'Téléphone' }}</a>
                    <a href="mailto:{{ $user->email }}"><i class="fas fa-envelope me-2"></i>{{ $user->email ?? 'Email' }}</a>
                </div>
            </div>
        </div>

        <div class="footer-bottom">
            <span>&copy; {{ date('Y') }} 
                @if($user->role === 'seller' && $user->shop_name)
                    {{ $user->shop_name }}
                @else
                    Boutique de {{ $user->name }}
                @endif
                . Tous droits réservés.
            </span>
        </div>
    </div>
</footer>

<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"></script>
<script src="https://unpkg.com/aos@2.3.1/dist/aos.js"></script>
<script>
    AOS.init({
        duration: 800,
        easing: 'ease-in-out',
        once: true
    });

    const maxStock = {{ $product->stock }};
    const productId = {{ $product->id }};
    const username = '{{ $user->username ?? $user->member_code ?? $user->id }}';

    function incrementQuantity() {
        const input = document.getElementById('quantity');
        if (parseInt(input.value) < maxStock) {
            input.value = parseInt(input.value) + 1;
        }
    }

    function decrementQuantity() {
        const input = document.getElementById('quantity');
        if (parseInt(input.value) > 1) {
            input.value = parseInt(input.value) - 1;
        }
    }

    async function addToCartDetail() {
        const btn = event.target.closest('.add-to-cart-btn');
        const quantity = parseInt(document.getElementById('quantity').value);
        const originalContent = btn.innerHTML;
        
        btn.disabled = true;
        btn.innerHTML = '<i class="fas fa-spinner fa-spin"></i> <span>Ajout en cours...</span>';

        try {
            const response = await fetch(`/u/${username}/add-to-cart`, {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json',
                    'X-CSRF-TOKEN': '{{ csrf_token() }}'
                },
                body: JSON.stringify({ product_id: productId, quantity: quantity })
            });

            const data = await response.json();

            if (data.success) {
                document.querySelector('.cart-badge').textContent = data.cart_count;
                
                btn.innerHTML = '<i class="fas fa-check"></i> <span>Ajouté au panier!</span>';
                btn.style.background = 'linear-gradient(135deg, #10b981, #059669)';
                
                setTimeout(() => {
                    window.location.href = `/u/${username}/cart`;
                }, 1500);
            } else {
                throw new Error(data.message || 'Erreur');
            }
        } catch (error) {
            console.error('Erreur:', error);
            alert(error.message || 'Erreur lors de l\'ajout au panier');
            btn.innerHTML = originalContent;
            btn.disabled = false;
        }
    }

    // Mettre à jour le compteur du panier
    async function updateCartCount() {
        try {
            const response = await fetch(`/u/${username}/cart-count`);
            const data = await response.json();
            document.querySelector('.cart-badge').textContent = data.count || 0;
        } catch (error) {
            console.error('Erreur compteur panier:', error);
        }
    }

    updateCartCount();
</script>

</body>
</html>
