<?php
require_once __DIR__ . '/functions.php';
$settings = getAllSettings();
$currentPage = basename($_SERVER['PHP_SELF'], '.php');

// Get navigation icon (favicon)
$navIconUrl = '';
if (!empty($settings['nav_icon'])) {
    $navIconPath = UPLOAD_PATH . 'general/' . $settings['nav_icon'];
    if (file_exists($navIconPath)) {
        $navIconUrl = UPLOAD_URL . 'general/' . $settings['nav_icon'];
    }
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    
    <?php // Meta Title (fallback to page title + site name) ?>
    <title><?= isset($pageTitle) && !empty($settings['meta_title']) ? $settings['meta_title'] : (isset($pageTitle) ? $pageTitle . ' - ' : '') . ($settings['site_name'] ?? 'Traveler') ?></title>
    
    <?php // Meta Description ?>
    <meta name="description" content="<?= $settings['meta_description'] ?? 'Discover amazing tour packages' ?>">
    
    <?php // Meta Keywords ?>
    <meta name="keywords" content="<?= $settings['meta_keywords'] ?? 'travel, tour, packages' ?>">
    
    <?php // Canonical URL ?>
    <link rel="canonical" href="<?= 'https://' . ($_SERVER['HTTP_HOST'] ?? 'yoursite.com') . htmlspecialchars($_SERVER['REQUEST_URI'], ENT_QUOTES, 'UTF-8') ?>">
    
    <?php // Search Engine Verification ?>
    <?php if (!empty($settings['google_verification'])): ?>
        <meta name="google-site-verification" content="<?= sanitize($settings['google_verification']) ?>">
    <?php endif; ?>
    
    <?php if (!empty($settings['bing_verification'])): ?>
        <meta name="msvalidate.01" content="<?= sanitize($settings['bing_verification']) ?>">
    <?php endif; ?>
    
    <?php if (!empty($settings['yandex_verification'])): ?>
        <meta name="yandex-verification" content="<?= sanitize($settings['yandex_verification']) ?>">
    <?php endif; ?>
    
    <?php if (!empty($settings['pinterest_verification'])): ?>
        <meta name="p:domain_verify" content="<?= sanitize($settings['pinterest_verification']) ?>">
    <?php endif; ?>
    
    <!-- Open Graph / Facebook -->
    <meta property="og:type" content="website">
    <meta property="og:url" content="<?= 'https://' . ($_SERVER['HTTP_HOST'] ?? 'yoursite.com') . htmlspecialchars($_SERVER['REQUEST_URI'], ENT_QUOTES, 'UTF-8') ?>">
    <meta property="og:title" content="<?= !empty($settings['meta_og_title']) ? sanitize($settings['meta_og_title']) : (isset($pageTitle) ? $pageTitle : ($settings['site_name'] ?? 'Traveler')) ?>">
    <meta property="og:description" content="<?= $settings['meta_og_description'] ?? ($settings['meta_description'] ?? 'Discover amazing tour packages') ?>">
    <?php if (!empty($settings['meta_og_image'])): 
        $ogImagePath = UPLOAD_PATH . 'general/' . $settings['meta_og_image'];
        if (file_exists($ogImagePath)):
    ?>
        <meta property="og:image" content="<?= htmlspecialchars(UPLOAD_URL . 'general/' . $settings['meta_og_image'], ENT_QUOTES, 'UTF-8') ?>">
    <?php endif; endif; ?>
    <meta property="og:site_name" content="<?= $settings['site_name'] ?? 'Traveler' ?>">
    
    <!-- Twitter Card -->
    <meta name="twitter:card" content="<?= $settings['meta_twitter_card'] ?? 'summary_large_image' ?>">
    <?php if (!empty($settings['meta_twitter_site'])): ?>
        <meta name="twitter:site" content="<?= sanitize($settings['meta_twitter_site']) ?>">
    <?php endif; ?>
    <meta name="twitter:title" content="<?= !empty($settings['meta_twitter_title']) ? sanitize($settings['meta_twitter_title']) : (isset($pageTitle) ? $pageTitle : ($settings['site_name'] ?? 'Traveler')) ?>">
    <meta name="twitter:description" content="<?= $settings['meta_twitter_description'] ?? ($settings['meta_description'] ?? 'Discover amazing tour packages') ?>">
    <?php if (!empty($settings['meta_twitter_image'])): 
        $twitterImagePath = UPLOAD_PATH . 'general/' . $settings['meta_twitter_image'];
        if (file_exists($twitterImagePath)):
    ?>
        <meta name="twitter:image" content="<?= htmlspecialchars(UPLOAD_URL . 'general/' . $settings['meta_twitter_image'], ENT_QUOTES, 'UTF-8') ?>">
    <?php endif; endif; ?>
    
    <!-- Favicon -->
    <?php if (!empty($navIconUrl)): ?>
        <link rel="icon" type="image/png" href="<?= htmlspecialchars($navIconUrl, ENT_QUOTES, 'UTF-8') ?>">
        <link rel="apple-touch-icon" href="<?= htmlspecialchars($navIconUrl, ENT_QUOTES, 'UTF-8') ?>">
    <?php else: ?>
        <link rel="icon" type="image/png" href="<?= BASE_URL ?>assets/images/favicon.png">
    <?php endif; ?>
    
    <!-- Google Analytics -->
    <?php if (!empty($settings['google_analytics_id'])): ?>
        <script async src="https://www.googletagmanager.com/gtag/js?id=<?= sanitize($settings['google_analytics_id']) ?>"></script>
        <script>
            window.dataLayer = window.dataLayer || [];
            function gtag(){dataLayer.push(arguments);}
            gtag('js', new Date());
            gtag('config', '<?= sanitize($settings['google_analytics_id']) ?>');
        </script>
    <?php endif; ?>
    
    <!-- Google Tag Manager -->
    <?php if (!empty($settings['google_tag_manager_id'])): ?>
        <script>(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':
        new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],
        j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=
        'https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);
        })(window,document,'script','dataLayer','<?= sanitize($settings['google_tag_manager_id']) ?>');</script>
    <?php endif; ?>
    
    <!-- Google 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=Poppins:wght@300;400;500;600;700;800&family=Playfair+Display:wght@400;500;600;700&display=swap" rel="stylesheet">
    
    <!-- Font Awesome 6 -->
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.1/css/all.min.css">
    
    <!-- Main CSS -->
    <link rel="stylesheet" href="<?= BASE_URL ?>assets/css/style.css">
    
    <?php if (isset($extraCSS)): ?>
        <?php foreach ($extraCSS as $css): ?>
            <link rel="stylesheet" href="<?= $css ?>">
        <?php endforeach; ?>
    <?php endif; ?>
    
    <?php // Schema.org JSON-LD Structured Data ?>
    <?= generateSchemaMarkup() ?>
    
    <?php // Legacy Google Analytics (from original settings) ?>
    <?php if (!empty($settings['google_analytics'])): ?>
        <?= $settings['google_analytics'] ?>
    <?php endif; ?>
</head>
<body>
    <!-- Navigation -->
    <nav class="navbar">
        <div class="container">
            <button class="nav-toggle mobile-menu-toggle mobile-only" aria-label="Toggle navigation">
                <i class="fas fa-bars"></i>
            </button>
            
            <a href="<?= BASE_URL ?>" class="navbar-brand">
                <?php if (($settings['logo_type'] ?? 'image') === 'text'): ?>
                    <?php $textLogo = $settings['text_logo'] ?? 'Traveler'; ?>
                    <?php $textLogoFont = $settings['text_logo_font'] ?? 'Playfair Display'; ?>
                    <?php $textLogoSize = $settings['text_logo_size'] ?? '24'; ?>
                    <?php $textLogoColor = $settings['text_logo_color'] ?? '#FF6B35'; ?>
                    <div class="brand-text text-logo" style="font-family: '<?= htmlspecialchars($textLogoFont, ENT_QUOTES, 'UTF-8') ?>', serif; font-size: <?= htmlspecialchars($textLogoSize, ENT_QUOTES, 'UTF-8') ?>px; color: <?= htmlspecialchars($textLogoColor, ENT_QUOTES, 'UTF-8') ?>;">
                        <?= htmlspecialchars($textLogo, ENT_QUOTES, 'UTF-8') ?>
                    </div>
                <?php else: ?>
                    <?php $logoImage = $settings['site_logo'] ?? ''; ?>
                    <?php if (!empty($logoImage) && file_exists(UPLOAD_PATH . 'general/' . $logoImage)): ?>
                        <img src="<?= htmlspecialchars(UPLOAD_URL . 'general/' . $logoImage, ENT_QUOTES, 'UTF-8') ?>" alt="<?= htmlspecialchars($settings['site_name'] ?? 'Logo', ENT_QUOTES, 'UTF-8') ?>" class="logo-image" style="max-height:40px;">
                    <?php else: ?>
                        <div class="brand-text">Travel<span>er</span></div>
                    <?php endif; ?>
                <?php endif; ?>
            </a>
            
            <div class="nav-menu">
                <a href="<?= BASE_URL ?>" class="<?= $currentPage === 'index' ? 'active' : '' ?>">Home</a>
                <a href="<?= BASE_URL ?>packages.php" class="<?= $currentPage === 'packages' ? 'active' : '' ?>">Packages</a>
                <a href="<?= BASE_URL ?>places.php" class="<?= $currentPage === 'places' ? 'active' : '' ?>">Destinations</a>
                <a href="<?= BASE_URL ?>about.php" class="<?= $currentPage === 'about' ? 'active' : '' ?>">About</a>
                <a href="<?= BASE_URL ?>contact.php" class="<?= $currentPage === 'contact' ? 'active' : '' ?>">Contact</a>
            </div>
            
            <div class="nav-actions">
                <?php if (isLoggedIn()): ?>
                    <?php $currentUser = getUserById($_SESSION['user_id']); ?>
                    <!-- Authenticated User Menu -->
                    <div class="user-menu">
                        <button class="user-menu-trigger" aria-label="User menu">
                            <div class="user-avatar">
                                <?php if (!empty($currentUser['profile_image'])): ?>
                                    <img src="<?= UPLOAD_URL ?>profiles/<?= htmlspecialchars($currentUser['profile_image']) ?>" alt="<?= htmlspecialchars($currentUser['name'] ?? 'User') ?>">
                                <?php else: ?>
                                    <span class="user-avatar-initials"><?= strtoupper(substr($currentUser['name'] ?? 'U', 0, 2)) ?></span>
                                <?php endif; ?>
                            </div>
                            <span class="user-menu-name desktop-only"><?= htmlspecialchars($currentUser['name'] ?? 'User') ?></span>
                            <i class="fas fa-chevron-down desktop-only"></i>
                        </button>
                        <div class="user-menu-dropdown">
                            <div class="user-menu-header">
                                <div class="user-menu-avatar">
                                    <?php if (!empty($currentUser['profile_image'])): ?>
                                        <img src="<?= UPLOAD_URL ?>profiles/<?= htmlspecialchars($currentUser['profile_image']) ?>" alt="<?= htmlspecialchars($currentUser['name'] ?? 'User') ?>">
                                    <?php else: ?>
                                        <span><?= strtoupper(substr($currentUser['name'] ?? 'U', 0, 2)) ?></span>
                                    <?php endif; ?>
                                </div>
                                <div class="user-menu-info">
                                    <span class="user-menu-name"><?= htmlspecialchars($currentUser['name'] ?? 'User') ?></span>
                                    <span class="user-menu-email"><?= htmlspecialchars($currentUser['email'] ?? '') ?></span>
                                </div>
                            </div>
                            <div class="user-menu-section">
                                <?php if (isAdmin()): ?>
                                    <a href="<?= ADMIN_URL ?>" class="user-menu-item">
                                        <i class="fas fa-cog"></i>
                                        <span>Admin Dashboard</span>
                                    </a>
                                <?php else: ?>
                                    <a href="<?= USER_URL ?>" class="user-menu-item">
                                        <i class="fas fa-user"></i>
                                        <span>My Account</span>
                                    </a>
                                    <a href="<?= USER_URL ?>bookings.php" class="user-menu-item">
                                        <i class="fas fa-calendar-alt"></i>
                                        <span>My Bookings</span>
                                    </a>
                                    <a href="<?= USER_URL ?>transactions.php" class="user-menu-item">
                                        <i class="fas fa-history"></i>
                                        <span>Transactions</span>
                                    </a>
                                    <a href="<?= USER_URL ?>settings.php" class="user-menu-item">
                                        <i class="fas fa-cog"></i>
                                        <span>Settings</span>
                                    </a>
                                <?php endif; ?>
                            </div>
                            <div class="user-menu-footer">
                                <a href="<?= BASE_URL ?>logout.php" class="user-menu-item logout">
                                    <i class="fas fa-sign-out-alt"></i>
                                    <span>Logout</span>
                                </a>
                            </div>
                        </div>
                    </div>
                <?php else: ?>
                    <!-- Unauthenticated User - Prominent Login/Signup Buttons -->
                    <a href="<?= BASE_URL ?>login.php" class="btn btn-outline btn-sm desktop-only">
                        <i class="fas fa-sign-in-alt"></i> Login
                    </a>
                    <a href="<?= BASE_URL ?>register.php" class="btn btn-primary btn-sm desktop-only">
                        <i class="fas fa-user-plus"></i> Sign Up
                    </a>
                <?php endif; ?>
            </div>
        </div>
    </nav>

    <!-- Mobile Slide-in Sidebar -->
    <div class="mobile-sidebar-overlay"></div>
    <div class="mobile-sidebar">
        <?php if (isLoggedIn()): ?>
            <?php $currentUser = getUserById($_SESSION['user_id']); ?>
            <div class="mobile-sidebar-user">
                <div class="mobile-sidebar-avatar">
                    <?php if (!empty($currentUser['profile_image'])): ?>
                        <img src="<?= UPLOAD_URL ?>profiles/<?= htmlspecialchars($currentUser['profile_image']) ?>" alt="<?= htmlspecialchars($currentUser['name'] ?? 'User') ?>">
                    <?php else: ?>
                        <span><?= strtoupper(substr($currentUser['name'] ?? 'U', 0, 2)) ?></span>
                    <?php endif; ?>
                </div>
                <div class="mobile-sidebar-user-info">
                    <span class="mobile-sidebar-user-name"><?= htmlspecialchars($currentUser['name'] ?? 'User') ?></span>
                    <span class="mobile-sidebar-user-email"><?= htmlspecialchars($currentUser['email'] ?? '') ?></span>
                </div>
            </div>
        <?php else: ?>
            <div class="mobile-sidebar-header">
                <span class="mobile-sidebar-title">
                    <?php if (($settings['logo_type'] ?? 'image') === 'text'): ?>
                        <?php $textLogo = $settings['text_logo'] ?? 'Traveler'; ?>
                        <?php $textLogoFont = $settings['text_logo_font'] ?? 'Playfair Display'; ?>
                        <?php $textLogoSize = $settings['text_logo_size'] ?? '24'; ?>
                        <?php $textLogoColor = $settings['text_logo_color'] ?? '#FF6B35'; ?>
                        <span class="mobile-sidebar-logo" style="font-family: '<?= htmlspecialchars($textLogoFont, ENT_QUOTES, 'UTF-8') ?>', serif; font-size: <?= htmlspecialchars($textLogoSize, ENT_QUOTES, 'UTF-8') ?>px; color: <?= htmlspecialchars($textLogoColor, ENT_QUOTES, 'UTF-8') ?>;">
                            <?= htmlspecialchars($textLogo, ENT_QUOTES, 'UTF-8') ?>
                        </span>
                    <?php else: ?>
                        <?php $logoImage = $settings['site_logo'] ?? ''; ?>
                        <?php if (!empty($logoImage) && file_exists(UPLOAD_PATH . 'general/' . $logoImage)): ?>
                            <img src="<?= htmlspecialchars(UPLOAD_URL . 'general/' . $logoImage, ENT_QUOTES, 'UTF-8') ?>" alt="<?= htmlspecialchars($settings['site_name'] ?? 'Logo', ENT_QUOTES, 'UTF-8') ?>" class="mobile-sidebar-logo-img" style="max-height:40px; max-width:150px; object-fit:contain;">
                        <?php else: ?>
                            <span class="mobile-sidebar-logo-text">Travel<span>er</span></span>
                        <?php endif; ?>
                    <?php endif; ?>
                </span>
                <button class="mobile-sidebar-close" aria-label="Close menu">
                    <i class="fas fa-times"></i>
                </button>
            </div>
        <?php endif; ?>
        
        <?php if (isLoggedIn()): ?>
            <button class="mobile-sidebar-close mobile-sidebar-close-user" aria-label="Close menu">
                <i class="fas fa-times"></i>
            </button>
        <?php endif; ?>
            
        <div class="mobile-sidebar-menu">
            <a href="<?= BASE_URL ?>" class="mobile-sidebar-link <?= $currentPage === 'index' ? 'active' : '' ?>">
                <i class="fas fa-home"></i> Home
            </a>
            <a href="<?= BASE_URL ?>packages.php" class="mobile-sidebar-link <?= $currentPage === 'packages' ? 'active' : '' ?>">
                <i class="fas fa-box"></i> Packages
            </a>
            <a href="<?= BASE_URL ?>places.php" class="mobile-sidebar-link <?= $currentPage === 'places' ? 'active' : '' ?>">
                <i class="fas fa-map-marker-alt"></i> Destinations
            </a>
            <a href="<?= BASE_URL ?>about.php" class="mobile-sidebar-link <?= $currentPage === 'about' ? 'active' : '' ?>">
                <i class="fas fa-info-circle"></i> About
            </a>
            <a href="<?= BASE_URL ?>contact.php" class="mobile-sidebar-link <?= $currentPage === 'contact' ? 'active' : '' ?>">
                <i class="fas fa-envelope"></i> Contact
            </a>
        </div>
        
        <?php if (isLoggedIn()): ?>
            <div class="mobile-sidebar-divider"></div>
            <div class="mobile-sidebar-menu mobile-sidebar-user-menu">
                <?php if (isAdmin()): ?>
                    <a href="<?= ADMIN_URL ?>" class="mobile-sidebar-link">
                        <i class="fas fa-cog"></i> Admin Dashboard
                    </a>
                <?php else: ?>
                    <a href="<?= USER_URL ?>" class="mobile-sidebar-link">
                        <i class="fas fa-user"></i> My Account
                    </a>
                    <a href="<?= USER_URL ?>bookings.php" class="mobile-sidebar-link">
                        <i class="fas fa-calendar-alt"></i> My Bookings
                    </a>
                    <a href="<?= USER_URL ?>transactions.php" class="mobile-sidebar-link">
                        <i class="fas fa-history"></i> Transactions
                    </a>
                    <a href="<?= USER_URL ?>settings.php" class="mobile-sidebar-link">
                        <i class="fas fa-cog"></i> Settings
                    </a>
                <?php endif; ?>
            </div>
        <?php endif; ?>
        
        <div class="mobile-sidebar-divider"></div>
        <div class="mobile-sidebar-auth">
            <?php if (isLoggedIn()): ?>
                <a href="<?= BASE_URL ?>logout.php" class="mobile-sidebar-btn btn btn-primary">
                    <i class="fas fa-sign-out-alt"></i> Logout
                </a>
            <?php else: ?>
                <a href="<?= BASE_URL ?>login.php" class="mobile-sidebar-btn btn btn-outline">
                    <i class="fas fa-sign-in-alt"></i> Login
                </a>
                <a href="<?= BASE_URL ?>register.php" class="mobile-sidebar-btn btn btn-primary">
                    <i class="fas fa-user-plus"></i> Register
                </a>
            <?php endif; ?>
        </div>
    </div>

    <!-- Flash Messages -->
    <?php $flash = getFlash(); ?>
    <?php if ($flash): ?>
        <div style="position:fixed;top:85px;right:20px;z-index:9999;min-width:300px;">
            <div class="alert alert-<?= $flash['type'] ?>">
                <i class="fas fa-<?= $flash['type'] === 'success' ? 'check-circle' : ($flash['type'] === 'danger' ? 'exclamation-circle' : 'info-circle') ?>"></i>
                <?= $flash['message'] ?>
                <button class="close-alert">&times;</button>
            </div>
        </div>
    <?php endif; ?>
