
document.addEventListener('DOMContentLoaded', () => {
    
    // --- 1. Age Verification (Standard for Vape Stores) ---
    const ageModal = document.getElementById('ageModal');
    const ageYes = document.getElementById('ageYes');
    const ageNo = document.getElementById('ageNo');

    // Check if already verified
    if(localStorage.getItem('ageVerified') === 'true') {
        ageModal.classList.remove('active');
        triggerNewsletter(); // Only show newsletter if age is verified
    }

    if(ageYes) {
        ageYes.addEventListener('click', () => {
            localStorage.setItem('ageVerified', 'true');
            ageModal.classList.remove('active');
            triggerNewsletter();
        });
    }

    if(ageNo) {
        ageNo.addEventListener('click', () => {
            alert('You must be 18 or older to access this site. Redirecting...');
            window.location.href = "https://www.google.com";
        });
    }


    // --- 2. Newsletter Modal (Auto Open) ---
    const newsletterModal = document.getElementById('newsletterModal');
    const closeBtn = document.querySelector('.close-modal');

    function triggerNewsletter() {
        // Show immediately if not seen before
        if(!sessionStorage.getItem('newsletterSeen')) {
            setTimeout(() => {
                newsletterModal.classList.add('active');
            }, 1000); // Small 1 sec delay for smooth experience
        }
    }

    if(closeBtn) {
        closeBtn.addEventListener('click', () => {
            newsletterModal.classList.remove('active');
            sessionStorage.setItem('newsletterSeen', 'true');
        });
    }

    const newsForm = document.querySelector('.newsletter-form');
    if(newsForm) {
        newsForm.addEventListener('submit', (e) => {
            e.preventDefault();
            alert('Thanks for subscribing!');
            newsletterModal.classList.remove('active');
            sessionStorage.setItem('newsletterSeen', 'true');
        });
    }


    // --- 3. Cookie Banner ---
    const cookieBanner = document.getElementById('cookieBanner');
    const acceptCookies = document.getElementById('acceptCookies');
    const rejectCookies = document.getElementById('rejectCookies');

    if(!localStorage.getItem('cookieConsent')) {
        setTimeout(() => {
            cookieBanner.classList.add('show');
        }, 2000);
    }

    if(acceptCookies) {
        acceptCookies.addEventListener('click', () => {
            localStorage.setItem('cookieConsent', 'accepted');
            cookieBanner.classList.remove('show');
        });
    }

    if(rejectCookies) {
        rejectCookies.addEventListener('click', () => {
            localStorage.setItem('cookieConsent', 'rejected');
            cookieBanner.classList.remove('show');
        });
    }


    // --- 4. Simple Cart Functionality ---
    let cart = JSON.parse(localStorage.getItem('mrSinghCart')) || [];
    updateCartCount();

    // Add to cart buttons
    const addBtns = document.querySelectorAll('.add-to-cart');
    addBtns.forEach(btn => {
        btn.addEventListener('click', () => {
            const name = btn.getAttribute('data-name');
            const price = parseFloat(btn.getAttribute('data-price'));
            
            cart.push({name, price});
            localStorage.setItem('mrSinghCart', JSON.stringify(cart));
            updateCartCount();
            
            // Visual feedback
            const originalText = btn.innerText;
            btn.innerText = "Added!";
            btn.style.backgroundColor = "#10b981"; // success green
            setTimeout(() => {
                btn.innerText = originalText;
                btn.style.backgroundColor = "";
            }, 1500);
        });
    });

    function updateCartCount() {
        const counts = document.querySelectorAll('.cart-count');
        counts.forEach(c => c.innerText = cart.length);
        renderCartPage();
    }

    // Render Cart Page if on it
    function renderCartPage() {
        const cartContainer = document.getElementById('cartItemsContainer');
        if(!cartContainer) return; // Not on cart page

        if(cart.length === 0) {
            cartContainer.innerHTML = `
                <div class="empty-cart-msg" style="text-align: center; padding: 40px 0;">
                    <i class="fa-solid fa-cart-shopping fa-3x" style="color: var(--border); margin-bottom: 20px;"></i>
                    <p>Your cart is currently empty.</p>
                    <a href="products.html" class="btn btn-outline" style="margin-top: 20px;">Continue Shopping</a>
                </div>
            `;
            document.getElementById('cartSubtotal').innerText = "£0.00";
            document.getElementById('cartTotal').innerText = "£0.00";
            return;
        }

        let html = '';
        let total = 0;

        cart.forEach((item, index) => {
            total += item.price;
            html += `
                <div class="cart-item">
                    <div>
                        <h4 style="margin-bottom: 5px;">${item.name}</h4>
                        <span style="color: var(--text-muted); font-size: 0.9rem;">Qty: 1</span>
                    </div>
                    <div style="display: flex; align-items: center; gap: 20px;">
                        <span style="font-family: 'Outfit'; font-weight: bold;">£${item.price.toFixed(2)}</span>
                        <button onclick="window.removeFromCart(${index})" style="background: none; border: none; color: var(--primary); cursor: pointer;"><i class="fa-solid fa-trash"></i></button>
                    </div>
                </div>
            `;
        });

        cartContainer.innerHTML = html;
        document.getElementById('cartSubtotal').innerText = "£" + total.toFixed(2);
        document.getElementById('cartTotal').innerText = "£" + total.toFixed(2);
    }

    // Global remove function for inline onclick
    window.removeFromCart = function(index) {
        cart.splice(index, 1);
        localStorage.setItem('mrSinghCart', JSON.stringify(cart));
        updateCartCount();
    }

    // Checkout button
    const checkoutBtn = document.querySelector('.checkout-btn');
    if(checkoutBtn) {
        checkoutBtn.addEventListener('click', () => {
            if(cart.length === 0) {
                alert("Your cart is empty!");
                return;
            }
            alert("This is a demo. In a live environment, this would redirect to Stripe, Snipcart, or Shopify checkout.");
        });
    }

});
