<?php
// admin/includes/header.php
if (!file_exists(__DIR__ . '/config.php')) {
    header("Location: install.php");
    exit;
}
require_once 'config.php';

// Verify database connection before proceeding
try {
    getDBConnection();
} catch (Exception $e) {
    // If we can't connect, but we are not on the install page, show a friendly error
    if (basename($_SERVER['PHP_SELF']) !== 'install.php') {
        die("
            <div style='font-family: sans-serif; padding: 50px; text-align: center; background: #f8fafc;'>
                <h1 style='color: #ef4444;'>Database Connection Error</h1>
                <p style='color: #64748b;'>Unable to connect to the database. Please check your credentials in <code>admin/includes/config.php</code>.</p>
                <p style='color: #94a3b8; font-size: 0.8em;'>Error: " . htmlspecialchars($e->getMessage()) . "</p>
                <a href='install.php' style='display: inline-block; margin-top: 20px; padding: 10px 20px; background: #3b82f6; color: white; text-decoration: none; rounded: 5px;'>Run Installer</a>
            </div>
        ");
    }
}

require_once 'branding.php';
require_once 'migrations.php';
if (!isLoggedIn() && basename($_SERVER['PHP_SELF']) !== 'login.php') {
   redirect('login.php');
}
$selected_currency = $_GET['currency'] ?? $_SESSION['currency'] ?? DEFAULT_CURRENCY;
$_SESSION['currency'] = $selected_currency;
?>
<!DOCTYPE html>
<html class="dark" lang="en">
<head>
    <meta charset="utf-8"/>
    <meta content="width=device-width, initial-scale=1.0" name="viewport"/>
    <title><?= htmlspecialchars($page_title ?? 'Admin') ?> - <?= htmlspecialchars(getBrandName()) ?></title>
    <link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700&display=swap" rel="stylesheet"/>
    <link href="https://fonts.googleapis.com/css2?family=Material+Symbols+Outlined:opsz,wght,FILL,GRAD@20..48,100..700,0..1,-50..200&display=block" rel="stylesheet"/>
    <script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
    <?php
    $base_path = "";
    include 'includes/tailwind_config_include.php';
    ?>
    <link rel="stylesheet" href="includes/responsive.css">
    <!-- Firebase SDK (Compat) -->
    <script src="https://www.gstatic.com/firebasejs/10.7.1/firebase-app-compat.js"></script>
    <script src="https://www.gstatic.com/firebasejs/10.7.1/firebase-database-compat.js"></script>
    <script src="https://www.gstatic.com/firebasejs/10.7.1/firebase-messaging-compat.js"></script>
    <script src="includes/firebase_config.js"></script>
    <script src="includes/notification_manager.js"></script>
    <style>
        body {
            font-family: 'Inter', sans-serif;
            -webkit-tap-highlight-color: transparent;
        }
        .no-scrollbar::-webkit-scrollbar { display: none; }
        .no-scrollbar { -ms-overflow-style: none; scrollbar-width: none; }
        .material-symbols-outlined {
            font-family: 'Material Symbols Outlined' !important;
            font-variation-settings: 'FILL' 0, 'wght' 400, 'GRAD' 0, 'opsz' 24;
            font-weight: normal;
            font-style: normal;
            font-size: 24px;
            line-height: 1;
            letter-spacing: normal;
            text-transform: none;
            display: inline-block;
            white-space: nowrap;
            word-wrap: normal;
            direction: ltr;
            -webkit-font-feature-settings: 'liga';
            -webkit-font-smoothing: antialiased;
        }
        .fill-1 { font-variation-settings: 'FILL' 1; }
    </style>
</head>
<body class="bg-background-light dark:bg-background-dark text-slate-900 dark:text-white transition-colors duration-200">
<div class="fixed inset-0 bg-black/50 z-40 hidden" id="drawer-overlay"></div>

<script>
document.addEventListener('DOMContentLoaded', function() {
    <?php if (isset($_SESSION['admin_id'])): ?>
    if (typeof NotificationManager !== 'undefined') {
        NotificationManager.init(<?= $_SESSION['admin_id'] ?>, 'admin');
    }
    <?php endif; ?>
});
</script>
