<?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" aria-label="Primary navigation">
            <div class="nav-group">
                <div class="nav-heading">
                    <div class="nav-label">Workspace</div>
                    <div class="nav-hint">Daily operations and shared tools.</div>
                </div>
                <div class="nav-card-list">
                    <?php if (can_access('dashboard.view')): ?>
                        <a class="nav-link <?= $navClass($routeIs(['dashboard/index'])) ?>" href="index.php">
                            <span class="nav-link-title">Dashboard</span>
                            <span class="nav-link-meta">Overview, health, and activity.</span>
                        </a>
                    <?php endif; ?>
                    <?php if (can_access('contacts.view')): ?>
                        <a class="nav-link <?= $navClass($routeStartsWith('contacts/')) ?>" href="index.php?route=contacts/index">
                            <span class="nav-link-title">Contacts</span>
                            <span class="nav-link-meta">Clients, lists, imports, and duplicate-safe contact management.</span>
                        </a>
                    <?php endif; ?>
                    <?php if (can_access('templates.view')): ?>
                        <a class="nav-link <?= $navClass($routeStartsWith('templates/')) ?>" href="index.php?route=templates/index">
                            <span class="nav-link-title">Templates</span>
                            <span class="nav-link-meta">Reusable SMS, MMS, and email content blocks.</span>
                        </a>
                    <?php endif; ?>
                    <?php if (can_access('dashboard.view')): ?>
                        <a class="nav-link <?= $navClass($routeStartsWith('search/')) ?>" href="index.php?route=search/index">
                            <span class="nav-link-title">Global Search</span>
                            <span class="nav-link-meta">Find contacts, messages, campaigns, and notes quickly.</span>
                        </a>
                    <?php endif; ?>
                </div>
            </div>

            <?php if (can_access('messages.view')): ?>
                <div class="nav-section">
                    <div class="nav-heading">
                        <div class="nav-label">Inbox</div>
                        <div class="nav-hint">One-to-one conversation workflows with larger click targets.</div>
                    </div>
                    <div class="nav-subnav">
                        <a class="nav-link <?= $navClass($routeIs(['messages/inboxSms', 'messages/index'])) ?>" href="index.php?route=messages/inboxSms">
                            <span class="nav-link-title">SMS/MMS</span>
                            <span class="nav-link-meta">Individual messages, threaded view, canned replies, and conversation workflow.</span>
                        </a>
                        <a class="nav-link <?= $navClass($routeIs(['messages/inboxEmail'])) ?>" href="index.php?route=messages/inboxEmail">
                            <span class="nav-link-title">Email</span>
                            <span class="nav-link-meta">Individual emails, threaded view, canned replies, and conversation workflow.</span>
                        </a>
                        <a class="nav-link <?= $navClass($routeIs(['messages/mailbox'])) ?>" href="index.php?route=messages/mailbox">
                            <span class="nav-link-title">Mailbox</span>
                            <span class="nav-link-meta">Inbox, Outbox, Sent, Junk, Trash, and custom folders for a Gmail-style email workspace.</span>
                        </a>
                    </div>
                </div>
            <?php endif; ?>

            <?php if (can_access('messages.campaigns')): ?>
                <div class="nav-section">
                    <div class="nav-heading">
                        <div class="nav-label">Campaigns</div>
                        <div class="nav-hint">Commercial-grade campaign planning, launch control, and reporting.</div>
                    </div>
                    <div class="nav-subnav">
                        <a class="nav-link <?= $navClass($routeIs(['messages/campaignDashboard'])) ?>" href="index.php?route=messages/campaignDashboard">
                            <span class="nav-link-title">Campaign Dashboard</span>
                            <span class="nav-link-meta">Delivery, bounce, suppression, health, trend, and performance signals.</span>
                        </a>
                        <a class="nav-link <?= $navClass($routeIs(['messages/campaignStudio'])) ?>" href="index.php?route=messages/campaignStudio">
                            <span class="nav-link-title">Campaign Studio</span>
                            <span class="nav-link-meta">Design campaigns, review studio readiness, and run preflight checks.</span>
                        </a>
                        <a class="nav-link <?= $navClass($routeIs(['messages/campaignReports', 'messages/report'])) ?>" href="index.php?route=messages/campaignReports">
                            <span class="nav-link-title">Campaign Reports</span>
                            <span class="nav-link-meta">Recovery queue, campaign queue, and reporting views.</span>
                        </a>
                    </div>
                </div>
            <?php endif; ?>

            <div class="nav-group">
                <div class="nav-heading">
                    <div class="nav-label">Admin</div>
                    <div class="nav-hint">Account, diagnostics, and deployment controls.</div>
                </div>
                <div class="nav-card-list">
                    <a class="nav-link <?= $navClass($routeIs(['settings/profile'])) ?>" href="index.php?route=settings/profile">
                        <span class="nav-link-title">My Profile</span>
                        <span class="nav-link-meta">User profile, preferences, and account details.</span>
                    </a>
                    <?php if (can_access('settings.view')): ?>
                        <a class="nav-link <?= $navClass($routeIs(['settings/index'])) ?>" href="index.php?route=settings/index">
                            <span class="nav-link-title">Settings &amp; Users</span>
                            <span class="nav-link-meta">Users, roles, providers, and application settings.</span>
                        </a>
                        <a class="nav-link <?= $navClass($routeIs(['settings/diagnostics', 'settings/providerStatus'])) ?>" href="index.php?route=settings/diagnostics">
                            <span class="nav-link-title">Diagnostics</span>
                            <span class="nav-link-meta">Provider tests, system checks, and schema visibility.</span>
                        </a>
                    <?php endif; ?>
                    <?php if (can_access('settings.manage')): ?>
                        <a class="nav-link" href="install.php#update-packages">
                            <span class="nav-link-title">Update Center</span>
                            <span class="nav-link-meta">Apply browser-based update packages with backups.</span>
                        </a>
                    <?php endif; ?>
                </div>
            </div>
        </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, campaigns, readiness, and diagnostics with larger sidebar targets, clearer grouping, and cleaner workspace navigation.</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; ?>
