<?php
start_session();
$user = current_user();
$currentRoute = $_GET['route'] ?? 'dashboard/index';
$routeIs = static function (array $routes) use ($currentRoute): bool {
    return in_array($currentRoute, $routes, true);
};
$routeStartsWith = static function (string $prefix) use ($currentRoute): bool {
    return str_starts_with($currentRoute, $prefix);
};
$navClass = static function (bool $active): string {
    return $active ? 'is-active' : '';
};
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title><?= e(config('app.name')) ?></title>
    <link rel="stylesheet" href="assets/css/style.css">
</head>
<body>
<div class="app-shell">
    <aside class="sidebar">
        <div class="brand-block">
            <div class="brand-kicker">Corporate Messaging Suite</div>
            <div class="brand">INS Communications</div>
        </div>
        <nav class="nav-menu">
            <?php if (can_access('dashboard.view')): ?>
                <a class="nav-link <?= $navClass($routeIs(['dashboard/index'])) ?>" href="index.php">Dashboard</a>
            <?php endif; ?>
            <?php if (can_access('contacts.view')): ?>
                <a class="nav-link <?= $navClass($routeStartsWith('contacts/')) ?>" href="index.php?route=contacts/index">Contacts</a>
            <?php endif; ?>
            <?php if (can_access('templates.view')): ?>
                <a class="nav-link <?= $navClass($routeStartsWith('templates/')) ?>" href="index.php?route=templates/index">Templates</a>
            <?php endif; ?>

            <?php if (can_access('messages.view')): ?>
                <div class="nav-section">
                    <div class="nav-label">Inbox</div>
                    <div class="nav-subnav">
                        <a class="nav-link <?= $navClass($routeIs(['messages/inboxSms', 'messages/index'])) ?>" href="index.php?route=messages/inboxSms">SMS/MMS</a>
                        <a class="nav-link <?= $navClass($routeIs(['messages/inboxEmail'])) ?>" href="index.php?route=messages/inboxEmail">Email</a>
                    </div>
                </div>
            <?php endif; ?>

            <?php if (can_access('messages.campaigns')): ?>
                <div class="nav-section">
                    <div class="nav-label">Campaigns</div>
                    <div class="nav-subnav">
                        <a class="nav-link <?= $navClass($routeIs(['messages/campaignDashboard'])) ?>" href="index.php?route=messages/campaignDashboard">Campaign Dashboard</a>
                        <a class="nav-link <?= $navClass($routeIs(['messages/campaignStudio'])) ?>" href="index.php?route=messages/campaignStudio">Campaign Studio</a>
                        <a class="nav-link <?= $navClass($routeIs(['messages/campaignReports', 'messages/report'])) ?>" href="index.php?route=messages/campaignReports">Campaign Reports</a>
                    </div>
                </div>
            <?php endif; ?>

            <?php if (can_access('dashboard.view')): ?>
                <a class="nav-link <?= $navClass($routeStartsWith('search/')) ?>" href="index.php?route=search/index">Global Search</a>
            <?php endif; ?>
            <a class="nav-link <?= $navClass($routeIs(['settings/profile'])) ?>" href="index.php?route=settings/profile">My Profile</a>
            <?php if (can_access('settings.view')): ?>
                <a class="nav-link <?= $navClass($routeIs(['settings/index'])) ?>" href="index.php?route=settings/index">Settings &amp; Users</a>
            <?php endif; ?>
            <?php if (can_access('settings.view')): ?>
                <a class="nav-link <?= $navClass($routeIs(['settings/diagnostics', 'settings/providerStatus'])) ?>" href="index.php?route=settings/diagnostics">Diagnostics</a>
            <?php endif; ?>
            <?php if (can_access('settings.manage')): ?>
                <a class="nav-link" href="install.php#update-packages">Update Center</a>
            <?php endif; ?>
        </nav>
        <div class="sidebar-footer">
            <div class="muted small"><?= e($user['name'] ?? '') ?></div>
            <div class="muted small"><?= e($user['role'] ?? '') ?></div>
            <a class="btn secondary full" href="index.php?route=auth/logout">Sign Out</a>
        </div>
    </aside>
    <main class="content">
        <header class="topbar">
            <div>
                <h1><?= e(config('app.name')) ?></h1>
                <p>Commercial-grade inbox and campaign operations with separated SMS/MMS, email, campaign dashboard, campaign studio, and campaign reporting workflows.</p>
            </div>
            <form method="get" action="index.php" style="display:flex;gap:8px;align-items:center;">
                <input type="hidden" name="route" value="search/index">
                <input type="search" name="q" value="<?= e($_GET['q'] ?? '') ?>" placeholder="Search everything" style="min-width:260px;">
                <button class="btn secondary" type="submit">Search</button>
            </form>
        </header>
        <?php if ($msg = flash('success')): ?><div class="alert success"><?= e(is_array($msg) ? json_encode($msg) : (string)$msg) ?></div><?php endif; ?>
        <?php if ($msg = flash('error')): ?><div class="alert error"><?= e(is_array($msg) ? json_encode($msg) : (string)$msg) ?></div><?php endif; ?>
