<?php
if (session_status() === PHP_SESSION_NONE) {
    session_start();
}

require_once __DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'config' . DIRECTORY_SEPARATOR . 'database.php';
require_once __DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'config' . DIRECTORY_SEPARATOR . 'config.php';

$auth = new Auth();
$auth->requireRole(['super_admin']);

$csrf = new CSRF();
$csrfToken = $csrf->getToken();

$currentPage = basename($_SERVER['SCRIPT_NAME'] ?? '', '.php');
$baseUrl = getBaseUrl();

$notification = new Notification();
$unreadCount = $notification->getUnreadCount($_SESSION['user_id']);

$user = $auth->user();
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="csrf-token" content="<?php echo sanitize($csrfToken); ?>">
    <title><?php echo isset($pageTitle) ? sanitize($pageTitle) . ' - ' : ''; ?>Super Admin - <?php echo sanitize(APP_NAME); ?></title>
    <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://cdn.datatables.net/1.13.6/css/dataTables.bootstrap5.min.css" rel="stylesheet">
    <link href="<?php echo $baseUrl; ?>/admin/assets/style.css" rel="stylesheet">
</head>
<body>
    <div class="wrapper">
        <nav id="sidebar" class="sidebar">
            <div class="sidebar-header">
                <a href="<?php echo $baseUrl; ?>/admin/" class="sidebar-brand">
                    <i class="fas fa-utensils"></i>
                    <span><?php echo sanitize(APP_NAME); ?></span>
                    <small class="text-muted d-block" style="font-size:11px;">Super Admin</small>
                </a>
            </div>
            <ul class="nav flex-column">
                <li class="nav-item">
                    <a class="nav-link <?php echo $currentPage === 'index' ? 'active' : ''; ?>" href="<?php echo $baseUrl; ?>/admin/">
                        <i class="fas fa-tachometer-alt"></i><span>Dashboard</span>
                    </a>
                </li>
                <li class="nav-item">
                    <a class="nav-link <?php echo in_array($currentPage, ['restaurants', 'restaurant_add', 'restaurant_edit', 'restaurant_view']) ? 'active' : ''; ?>" href="<?php echo $baseUrl; ?>/admin/restaurants.php">
                        <i class="fas fa-store"></i><span>Restaurants</span>
                    </a>
                </li>
                <li class="nav-item">
                    <a class="nav-link <?php echo $currentPage === 'plans' ? 'active' : ''; ?>" href="<?php echo $baseUrl; ?>/admin/plans.php">
                        <i class="fas fa-crown"></i><span>Subscription Plans</span>
                    </a>
                </li>
                <li class="nav-item">
                    <a class="nav-link <?php echo $currentPage === 'users' ? 'active' : ''; ?>" href="<?php echo $baseUrl; ?>/admin/users.php">
                        <i class="fas fa-users"></i><span>Users</span>
                    </a>
                </li>
                <li class="nav-item">
                    <a class="nav-link <?php echo $currentPage === 'enquiries' ? 'active' : ''; ?>" href="<?php echo $baseUrl; ?>/admin/enquiries.php">
                        <i class="fas fa-envelope"></i><span>Enquiries</span>
                    </a>
                </li>
                <li class="nav-item">
                    <a class="nav-link <?php echo $currentPage === 'cms' ? 'active' : ''; ?>" href="<?php echo $baseUrl; ?>/admin/cms.php">
                        <i class="fas fa-globe"></i><span>Website CMS</span>
                    </a>
                </li>
                <li class="nav-item">
                    <a class="nav-link <?php echo $currentPage === 'pricing' ? 'active' : ''; ?>" href="<?php echo $baseUrl; ?>/admin/pricing.php">
                        <i class="fas fa-tags"></i><span>Pricing Plans</span>
                    </a>
                </li>
                <li class="nav-item">
                    <a class="nav-link <?php echo $currentPage === 'testimonials' ? 'active' : ''; ?>" href="<?php echo $baseUrl; ?>/admin/testimonials.php">
                        <i class="fas fa-quote-right"></i><span>Testimonials</span>
                    </a>
                </li>
                <li class="nav-item">
                    <a class="nav-link <?php echo $currentPage === 'faqs' ? 'active' : ''; ?>" href="<?php echo $baseUrl; ?>/admin/faqs.php">
                        <i class="fas fa-question-circle"></i><span>FAQs</span>
                    </a>
                </li>
                <li class="nav-item">
                    <a class="nav-link <?php echo $currentPage === 'demo_data' ? 'active' : ''; ?>" href="<?php echo $baseUrl; ?>/admin/demo_data.php">
                        <i class="fas fa-flask"></i><span>Demo Data</span>
                    </a>
                </li>
                <li class="nav-item">
                    <a class="nav-link <?php echo $currentPage === 'settings' ? 'active' : ''; ?>" href="<?php echo $baseUrl; ?>/admin/settings.php">
                        <i class="fas fa-cog"></i><span>System Settings</span>
                    </a>
                </li>
                <li class="nav-item">
                    <a class="nav-link <?php echo $currentPage === 'activity_logs' ? 'active' : ''; ?>" href="<?php echo $baseUrl; ?>/admin/activity_logs.php">
                        <i class="fas fa-history"></i><span>Activity Logs</span>
                    </a>
                </li>
                <li class="nav-item">
                    <a class="nav-link <?php echo $currentPage === 'error_logs' ? 'active' : ''; ?>" href="<?php echo $baseUrl; ?>/admin/error_logs.php">
                        <i class="fas fa-exclamation-triangle"></i><span>Error Logs</span>
                    </a>
                </li>
            </ul>
        </nav>

        <div id="content" class="content">
            <nav class="navbar navbar-expand-lg navbar-light bg-white shadow-sm mb-3">
                <div class="d-flex align-items-center">
                    <button type="button" id="sidebarToggle" class="btn btn-sm btn-outline-secondary me-2">
                        <i class="fas fa-bars"></i>
                    </button>
                    <h5 class="mb-0 d-none d-md-block"><?php echo isset($pageTitle) ? sanitize($pageTitle) : 'Dashboard'; ?></h5>
                </div>
                <div class="ms-auto d-flex align-items-center">
                    <div class="position-relative me-3 d-none d-md-block">
                        <form class="d-flex" action="<?php echo $baseUrl; ?>/admin/restaurants.php" method="GET">
                            <input class="form-control form-control-sm" type="search" name="search" placeholder="Search..." style="width:200px;">
                        </form>
                    </div>
                    <div class="dropdown me-2">
                        <a href="#" class="btn btn-sm btn-outline-secondary position-relative" id="notifDropdown" data-bs-toggle="dropdown" aria-expanded="false">
                            <i class="fas fa-bell"></i>
                            <span class="position-absolute top-0 start-100 translate-middle badge rounded-pill bg-danger notification-badge" style="display:<?php echo $unreadCount > 0 ? 'inline-block' : 'none'; ?>">
                                <?php echo $unreadCount; ?>
                            </span>
                        </a>
                        <div class="dropdown-menu dropdown-menu-end notification-dropdown" style="width:320px; max-height:400px; overflow-y:auto;">
                            <h6 class="dropdown-header">Notifications</h6>
                            <div id="notificationList">
                                <div class="text-center p-3 text-muted">Loading...</div>
                            </div>
                            <div class="dropdown-divider"></div>
                            <a class="dropdown-item text-center small" href="#" id="markAllRead">Mark all as read</a>
                        </div>
                    </div>
                    <div class="dropdown">
                        <a href="#" class="d-flex align-items-center text-decoration-none dropdown-toggle" data-bs-toggle="dropdown">
                            <div class="avatar-circle me-2"><?php echo strtoupper(substr($user['name'], 0, 1)); ?></div>
                            <span class="d-none d-md-inline"><?php echo sanitize($user['name']); ?></span>
                        </a>
                        <ul class="dropdown-menu dropdown-menu-end">
                            <li><a class="dropdown-item" href="<?php echo $baseUrl; ?>/admin/profile.php"><i class="fas fa-user me-2"></i>Profile</a></li>
                            <li><a class="dropdown-item" href="<?php echo $baseUrl; ?>/admin/profile.php#change-password"><i class="fas fa-lock me-2"></i>Change Password</a></li>
                            <li><hr class="dropdown-divider"></li>
                            <li><a class="dropdown-item text-danger" href="<?php echo $baseUrl; ?>/logout.php"><i class="fas fa-sign-out-alt me-2"></i>Logout</a></li>
                        </ul>
                    </div>
                </div>
            </nav>
            <div class="container-fluid">
