<?php
/**
 * Tom's Java Jive - Header Include
 */

if (session_status() === PHP_SESSION_NONE) {
    session_start();
}
header('Cache-Control: no-store, no-cache, must-revalidate, no-transform');

require_once __DIR__ . '/db.php';
require_once __DIR__ . '/../config/config.php';
require_once __DIR__ . '/auth.php';

$cartCount = 0;
$cart = $_SESSION['cart'] ?? [];
foreach ($cart as $qty) {
    $cartCount += $qty;
}

$isLoggedIn = CustomerAuth::isLoggedIn();
$customerUser = $isLoggedIn ? CustomerAuth::getUser() : null;
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title><?= $pageTitle ?? SITE_NAME ?></title>
    <!-- SEO Meta Tags -->
    <meta name="description" content="<?= htmlspecialchars($metaDescription ?? "Premium artisan coffee beans freshly roasted and delivered to your door. Shop single origin, blends, and specialty coffee from Tom's Java Jive.") ?>">
    <meta name="keywords" content="<?= htmlspecialchars($metaKeywords ?? "artisan coffee, coffee beans, single origin coffee, fresh roasted coffee, Weatherford Texas coffee, specialty coffee") ?>">
    <meta name="robots" content="<?= $metaRobots ?? 'index, follow' ?>">
    <!-- Google Search Console verification -->
    <meta name="google-site-verification" content="8alKRGXrSdoMHfdwDh9sfpfycSbNCIV8HCUcKQtNI_s">
    <link rel="canonical" href="<?= $canonicalUrl ?? 'https://tomsjavajive.com' . strtok($_SERVER['REQUEST_URI'], '?') ?>">
    <meta property="og:type" content="<?= $ogType ?? 'website' ?>">
    <meta property="og:site_name" content="Tom's Java Jive">
    <meta property="og:title" content="<?= htmlspecialchars($metaTitle ?? ($pageTitle ?? "Tom's Java Jive")) ?>">
    <meta property="og:description" content="<?= htmlspecialchars($metaDescription ?? "Premium artisan coffee beans freshly roasted and delivered to your door.") ?>">
    <meta property="og:url" content="<?= $canonicalUrl ?? 'https://tomsjavajive.com' . $_SERVER['REQUEST_URI'] ?>">
    <meta property="og:image" content="<?= $ogImage ?? 'https://tomsjavajive.com/assets/images/og-image.jpg' ?>">
    <meta property="og:locale" content="en_US">
    <meta name="twitter:card" content="summary_large_image">
    <meta name="twitter:title" content="<?= htmlspecialchars($metaTitle ?? ($pageTitle ?? "Tom's Java Jive")) ?>">
    <meta name="twitter:description" content="<?= htmlspecialchars($metaDescription ?? "Premium artisan coffee beans freshly roasted and delivered to your door.") ?>">
    <meta name="twitter:image" content="<?= $ogImage ?? 'https://tomsjavajive.com/assets/images/og-image.jpg' ?>">
    <?php if (empty($suppressSchema)): ?>
    <script type="application/ld+json">
    {"@context":"https://schema.org","@graph":[{"@type":["LocalBusiness","Store","FoodEstablishment"],"@id":"https://tomsjavajive.com/#business","name":"Tom's Java Jive","description":"Premium artisan coffee beans freshly roasted and shipped to your door. Specializing in single origin, signature blends, and specialty roasts from Weatherford, Texas.","url":"https://tomsjavajive.com","telephone":"+18172662022","email":"sales@tomsjavajive.com","priceRange":"$$","currenciesAccepted":"USD","paymentAccepted":"Credit Card, PayPal","servesCuisine":"Coffee","address":{"@type":"PostalAddress","addressLocality":"Weatherford","addressRegion":"TX","postalCode":"76088","addressCountry":"US"},"geo":{"@type":"GeoCoordinates","latitude":32.7554,"longitude":-97.7981},"hasOfferCatalog":{"@type":"OfferCatalog","name":"Coffee Products","itemListElement":[{"@type":"Offer","itemOffered":{"@type":"Product","name":"Single Origin Coffee Beans","description":"Hand-selected single origin beans from around the world, freshly roasted to order."}},{"@type":"Offer","itemOffered":{"@type":"Product","name":"Signature Blend Coffee","description":"Tom's signature house blends crafted for balanced, full-flavored cups."}}]}},{"@type":"WebSite","@id":"https://tomsjavajive.com/#website","url":"https://tomsjavajive.com","name":"Tom's Java Jive","publisher":{"@id":"https://tomsjavajive.com/#business"},"potentialAction":{"@type":"SearchAction","target":"https://tomsjavajive.com/shop.php?search={search_term_string}","query-input":"required name=search_term_string"}}]}
    </script>
    <?php endif; ?>
    <?php if (!empty($breadcrumbs)): ?>
    <script type="application/ld+json">
    {"@context":"https://schema.org","@type":"BreadcrumbList","itemListElement":<?= json_encode(array_map(function($b,$i){return["@type"=>"ListItem","position"=>$i+1,"name"=>$b['name'],"item"=>$b['url']];},$breadcrumbs,array_keys($breadcrumbs)),JSON_UNESCAPED_SLASHES) ?>}
    </script>
    <?php endif; ?>
    <?php if (!empty($productSchema)): ?>
    <script type="application/ld+json"><?= $productSchema ?></script>
    <?php endif; ?>

    <!-- PWA Meta Tags -->
    <meta name="theme-color" content="#FF5E1A">
    <meta name="mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="default">
    <meta name="apple-mobile-web-app-title" content="Java Jive">
    <link rel="manifest" href="/manifest.json">
    <link rel="apple-touch-icon" href="/assets/icons/icon-192.png">
    
    <!-- Fonts -->
    <link rel="preconnect" href="https://fonts.googleapis.com">
    <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
    <link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700&family=Playfair+Display:wght@600;700&display=swap" rel="stylesheet">
    
    <!-- Icons -->
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.1/css/all.min.css">
    
    <!-- Styles -->
    <link rel="stylesheet" href="/assets/css/style.css?v=<?= filemtime($_SERVER['DOCUMENT_ROOT'] . '/assets/css/style.css') ?>">
    
    <!-- Favicon -->
    <link rel="icon" type="image/x-icon" href="/favicon.ico">
    <link rel="icon" type="image/png" sizes="192x192" href="/assets/images/icon-192.png">
    <link rel="apple-touch-icon" sizes="192x192" href="/assets/images/icon-192.png">

    <?php if (strpos($_SERVER['REQUEST_URI'], 'payment') !== false || strpos($_SERVER['REQUEST_URI'], 'checkout') !== false): ?>
    <script src="https://js.stripe.com/v3/"></script>
    <?php endif; ?>
    
    <?php if (isset($extraHead)) echo $extraHead; ?>
</head>
<body>
    <header class="header">
        <nav class="nav container">
            <a href="/" class="logo">
                <img src="/assets/images/logo.png?v=<?= filemtime($_SERVER['DOCUMENT_ROOT'] . '/assets/images/logo.png') ?>" alt="<?= SITE_NAME ?>" class="logo-img">
            </a>
            
            <ul class="nav-links">
                <li><a href="/" class="<?= $_SERVER['REQUEST_URI'] === '/' ? 'active' : '' ?>">Home</a></li>
                <li><a href="/shop.php" class="<?= strpos($_SERVER['REQUEST_URI'], 'shop') !== false ? 'active' : '' ?>">Shop</a></li>
                <li><a href="/#about">About</a></li>
                <?php if ($isLoggedIn): ?>
                    <li><a href="/account/" class="<?= strpos($_SERVER['REQUEST_URI'], 'account') !== false ? 'active' : '' ?>">Account</a></li>
                <?php else: ?>
                    <li><a href="/login.php">Account</a></li>
                <?php endif; ?>
            </ul>
            
            <div class="nav-actions">
                <a href="/cart.php" class="cart-btn" title="Shopping Cart">
                    <i class="fas fa-shopping-bag"></i>
                    <?php if ($cartCount > 0): ?>
                        <span class="cart-count"><?= $cartCount ?></span>
                    <?php endif; ?>
                </a>
            </div>
        </nav>
    </header>
    
    <main>
