<?php
/**
 * Frontend Header - Premium Version
 * Digital Product Store
 */

// Start session if not started
if (session_status() === PHP_SESSION_NONE) {
    session_start();
}

/**
 * ✅ FIXED: CORRECT PATHS
 */
require_once __DIR__ . '/../config/database.php';

// Load functions safely
$functions_file = __DIR__ . '/functions.php';
if (file_exists($functions_file)) {
    require_once $functions_file;
}

/**
 * ✅ SAFETY FALLBACKS (PREVENT CRASHES)
 */

// Cart count fallback
if (!function_exists('get_cart_count')) {
    function get_cart_count() {
        return 0;
    }
}

// Categories fallback
if (!function_exists('get_categories')) {
    function get_categories() {
        return [];
    }
}

// Theme fallback
if (!function_exists('get_theme_colors')) {
    function get_theme_colors($pdo = null) {
        return [
            'primary_color' => '#6366f1',
            'secondary_color' => '#1f2937',
            'success_color' => '#10b981',
            'info_color' => '#3b82f6',
            'warning_color' => '#f59e0b',
            'danger_color' => '#ef4444'
        ];
    }
}

// Settings fallback
if (!function_exists('get_setting')) {
    function get_setting($key, $default = '') {
        return $default;
    }
}

// Auth fallback
if (!function_exists('is_user_logged_in')) {
    function is_user_logged_in() {
        return isset($_SESSION['user_id']);
    }
}

// Flash message fallback
if (!function_exists('get_flash_message')) {
    function get_flash_message() {
        return '';
    }
}

/**
 * ✅ SAFE VARIABLE INITIALIZATION
 */

// Ensure PDO exists
$pdo = $pdo ?? null;

// Get cart count
$cart_count = get_cart_count();

// Get categories
$nav_categories = get_categories();

// Get theme colors safely
$theme_colors = get_theme_colors($pdo);

// Page title fallback
if (!isset($page_title)) {
    $page_title = get_setting('site_name', 'Digital Store');
}

// Current page
$current_page = basename($_SERVER['PHP_SELF'], '.php');

// Ensure BASE_URL exists
if (!defined('BASE_URL')) {
    define('BASE_URL', '');
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title><?php echo htmlspecialchars($page_title); ?> - <?php echo get_setting('site_name', 'Digital Store'); ?></title>
    
    <meta name="description" content="<?php echo htmlspecialchars(get_setting('seo_description', 'Buy premium digital products at affordable prices')); ?>">
    <meta name="keywords" content="<?php echo htmlspecialchars(get_setting('seo_keywords', 'digital products, software, downloads, ebooks')); ?>">
    
    <!-- CSS -->
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css2?family=Inter:wght@300;400;500;600;700;800&display=swap" rel="stylesheet">
    <link href="<?php echo BASE_URL; ?>/assets/css/style.css" rel="stylesheet">

    <style>
        :root {
            --primary-color: <?php echo $theme_colors['primary_color']; ?>;
            --secondary-color: <?php echo $theme_colors['secondary_color']; ?>;
            --success-color: <?php echo $theme_colors['success_color']; ?>;
            --info-color: <?php echo $theme_colors['info_color']; ?>;
            --warning-color: <?php echo $theme_colors['warning_color']; ?>;
            --danger-color: <?php echo $theme_colors['danger_color']; ?>;
        }
    </style>
</head>
<body>

<!-- NAVBAR -->
<nav class="navbar navbar-expand-lg navbar-dark sticky-top" style="background: linear-gradient(135deg, #1a1a2e 0%, #16213e 100%);">
    <div class="container">
        <a class="navbar-brand fw-bold" href="<?php echo BASE_URL; ?>/index.php">
            <i class="fas fa-store me-2"></i><?php echo get_setting('site_name', 'Digital Store'); ?>
        </a>

        <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNav">
            <span class="navbar-toggler-icon"></span>
        </button>

        <div class="collapse navbar-collapse" id="navbarNav">

            <!-- LEFT -->
            <ul class="navbar-nav me-auto">
                <li class="nav-item">
                    <a class="nav-link <?php echo $current_page === 'index' ? 'active' : ''; ?>" href="<?php echo BASE_URL; ?>/index.php">Home</a>
                </li>

                <li class="nav-item">
                    <a class="nav-link <?php echo $current_page === 'products' ? 'active' : ''; ?>" href="<?php echo BASE_URL; ?>/products.php">Products</a>
                </li>

                <!-- Categories -->
                <li class="nav-item dropdown">
                    <a class="nav-link dropdown-toggle" href="#" data-bs-toggle="dropdown">Categories</a>
                    <ul class="dropdown-menu dropdown-menu-dark">
                        <?php foreach (array_slice($nav_categories, 0, 8) as $cat): ?>
                            <li>
                                <a class="dropdown-item" href="<?php echo BASE_URL; ?>/products.php?category=<?php echo $cat['slug'] ?? ''; ?>">
                                    <?php echo htmlspecialchars($cat['name'] ?? 'Category'); ?>
                                </a>
                            </li>
                        <?php endforeach; ?>
                    </ul>
                </li>
            </ul>

            <!-- RIGHT -->
            <ul class="navbar-nav ms-auto align-items-center">

                <!-- Cart -->
                <li class="nav-item me-3">
                    <a class="nav-link position-relative" href="<?php echo BASE_URL; ?>/cart.php">
                        <i class="fas fa-shopping-cart"></i>
                        <?php if ($cart_count > 0): ?>
                            <span class="badge bg-danger position-absolute top-0 start-100 translate-middle">
                                <?php echo $cart_count; ?>
                            </span>
                        <?php endif; ?>
                    </a>
                </li>

                <!-- Auth -->
                <?php if (is_user_logged_in()): ?>
                    <li class="nav-item">
                        <a class="nav-link" href="<?php echo BASE_URL; ?>/account/index.php">My Account</a>
                    </li>
                <?php else: ?>
                    <li class="nav-item">
                        <a class="btn btn-outline-light btn-sm" href="<?php echo BASE_URL; ?>/login.php">Login</a>
                    </li>
                <?php endif; ?>

            </ul>
        </div>
    </div>
</nav>

<!-- Notification -->
<?php if (get_setting('notification_enabled', '0') === '1'): ?>
    <div class="alert text-center m-0">
        <?php echo get_setting('notification_text', ''); ?>
    </div>
<?php endif; ?>

<!-- Flash -->
<?php echo get_flash_message(); ?>