<?php
/**
 * header.php — Authenticated page layout header (Material Design 3)
 */

ini_set('display_errors', '0');

// Only include functions.php if not already loaded (auth.php may have loaded it)
if (!isset($db)) {
    include(__DIR__ . '/functions.php');
}

require_login();

$currentPage = basename($_SERVER['SCRIPT_NAME'], '.php');
$panelName = $config_ini['titles']['panel_name'] ?? 'IPTV Panel';
$contactUrl = $config_ini['titles']['contact'] ?? '#';
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title><?= htmlspecialchars($panelName) ?></title>
    <link rel="icon" type="image/x-icon" href="./img/favicon.ico">
    <link rel="apple-touch-icon" sizes="180x180" href="./img/apple-touch-icon.png">
    <link rel="icon" type="image/png" sizes="32x32" href="./img/favicon-32x32.png">
    <link rel="icon" type="image/png" sizes="16x16" href="./img/favicon-16x16.png">
    <!-- Material Design 3 via Material Web Components -->
    <link href="https://fonts.googleapis.com/css2?family=Roboto:wght@300;400;500;700&display=swap" rel="stylesheet">
    <link href="css/material.css" rel="stylesheet">
</head>
<body class="md-body">
    <!-- Navigation Rail / Drawer -->
    <aside class="md-nav-drawer" id="navDrawer">
        <div class="md-nav-drawer__header">
            <img src="./img/login_logo.png" alt="Logo" class="md-nav-drawer__logo">
            <span class="md-nav-drawer__title"><?= htmlspecialchars($panelName) ?></span>
        </div>
        <nav class="md-nav-drawer__items">
            <a href="main.php" class="md-nav-item <?= $currentPage === 'main' ? 'md-nav-item--active' : '' ?>">
                <?= icon('dns') ?>
                <span class="md-nav-item__label">DNS Settings</span>
            </a>
            <a href="welcome.php" class="md-nav-item <?= $currentPage === 'welcome' ? 'md-nav-item--active' : '' ?>">
                <?= icon('chat') ?>
                <span class="md-nav-item__label">Welcome Messages</span>
            </a>
            <a href="devices.php" class="md-nav-item <?= $currentPage === 'devices' ? 'md-nav-item--active' : '' ?>">
                <?= icon('devices') ?>
                <span class="md-nav-item__label">Devices</span>
            </a>
            <a href="analytics.php" class="md-nav-item <?= $currentPage === 'analytics' ? 'md-nav-item--active' : '' ?>">
                <?= icon('monitoring') ?>
                <span class="md-nav-item__label">Analytics</span>
            </a>
            <a href="note.php" class="md-nav-item <?= $currentPage === 'note' ? 'md-nav-item--active' : '' ?>">
                <?= icon('campaign') ?>
                <span class="md-nav-item__label">Announcements</span>
            </a>
            <a href="vpn.php" class="md-nav-item <?= $currentPage === 'vpn' ? 'md-nav-item--active' : '' ?>">
                <?= icon('vpn_key') ?>
                <span class="md-nav-item__label">VPN Profiles</span>
            </a>
            <a href="sports.php" class="md-nav-item <?= $currentPage === 'sports' ? 'md-nav-item--active' : '' ?>">
                <?= icon('sports_soccer') ?>
                <span class="md-nav-item__label">Sports</span>
            </a>
            <div class="md-nav-divider"></div>
            <a href="user.php" class="md-nav-item <?= $currentPage === 'user' ? 'md-nav-item--active' : '' ?>">
                <?= icon('manage_accounts') ?>
                <span class="md-nav-item__label">Credentials</span>
            </a>
            <a href="security.php" class="md-nav-item <?= $currentPage === 'security' ? 'md-nav-item--active' : '' ?>">
                <?= icon('fingerprint') ?>
                <span class="md-nav-item__label">Passkeys</span>
            </a>
        </nav>
        <div class="md-nav-drawer__footer">
            <a href="<?= htmlspecialchars($contactUrl) ?>" target="_blank" class="md-nav-item">
                <?= icon('support') ?>
                <span class="md-nav-item__label">Support</span>
            </a>
            <div class="app-footer">
                <span>Made with <span class="app-footer__heart" aria-label="love">&hearts;</span> by <a href="https://krankhaus.ca" target="_blank" rel="noopener noreferrer">Krankhaus</a></span>
            </div>
        </div>
    </aside>

    <!-- Main Content Area -->
    <main class="md-main">
        <!-- Top App Bar -->
        <header class="md-top-app-bar">
            <button class="md-icon-button" id="menuToggle" aria-label="Toggle menu">
                <?= icon('menu') ?>
            </button>
            <h1 class="md-top-app-bar__title"><?= htmlspecialchars($panelName) ?></h1>
            <div class="md-top-app-bar__actions">
                <span class="md-top-app-bar__user">
                    <?= icon('account_circle') ?>
                    <?= htmlspecialchars($_SESSION['name'] ?? '') ?>
                </span>
                <a href="<?= basename($_SERVER['SCRIPT_NAME']) ?>?logout" class="md-button md-button--text md-button--error" title="Logout">
                    <?= icon('logout') ?>
                    <span class="md-button__label">Logout</span>
                </a>
            </div>
        </header>

        <!-- Page Content -->
        <div class="md-content">
            <div id="snackbarContainer"></div>
