<?php
use App\Core\Auth\Services\AuthService;
use App\Core\Modules\Services\ModuleManager;

$auth = new AuthService();
$user = $auth->user();
$moduleMenus = (new ModuleManager())->menus();
$currentPath = trim(uri_string(), '/');
$isActiveRoute = static fn (string $route): string => str_starts_with($currentPath, trim($route, '/')) ? ' active' : '';
$coreMenu = [
    ['label' => 'Dashboard', 'route' => '/dashboard', 'permission' => 'core.dashboard.view', 'icon' => 'speedometer2'],
    ['label' => 'Utilisateurs', 'route' => '/users', 'permission' => 'core.users.manage', 'icon' => 'people'],
    ['label' => 'Rôles', 'route' => '/roles', 'permission' => 'core.roles.manage', 'icon' => 'shield-lock'],
    ['label' => 'Modules', 'route' => '/modules', 'permission' => 'core.modules.manage', 'icon' => 'grid'],
    ['label' => 'Connexions SQL', 'route' => '/database-connections', 'permission' => 'core.database.manage', 'icon' => 'database'],
    ['label' => 'Historique IA', 'route' => '/ai-history', 'permission' => 'core.ai.use', 'icon' => 'stars'],
    ['label' => 'Audit', 'route' => '/audit', 'permission' => 'core.audit.view', 'icon' => 'activity'],
    ['label' => 'Paramètres', 'route' => '/settings', 'permission' => 'core.settings.manage', 'icon' => 'gear'],
];
?>
<!doctype html>
<html lang="fr">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title><?= esc($title ?? 'ONE CORE') ?> - ONE CORE</title>
    <link rel="icon" type="image/png" href="<?= base_url('assets/brand/one-core-favicon.png') ?>">
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
    <link href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.3/font/bootstrap-icons.css" rel="stylesheet">
    <style>
        :root {
            --core-navy: #0b1220;
            --core-blue: #1348ff;
            --core-cyan: #16c8ff;
            --core-soft: #f4f7fb;
        }

        body { background: var(--core-soft); color: #172033; }
        .core-shell { min-height: 100vh; display: grid; grid-template-columns: 280px 1fr; }
        .core-sidebar { background: linear-gradient(180deg, var(--core-navy), #111c32); color: #fff; padding: 24px; }
        .core-brand { font-weight: 800; letter-spacing: .06em; color: #fff; text-decoration: none; display: flex; align-items: center; gap: 12px; }
        .core-brand-logo { width: 48px; height: 48px; border-radius: 8px; object-fit: cover; filter: drop-shadow(0 0 3px rgba(255,255,255,1)); }
        .core-nav a { color: rgba(255,255,255,.78); text-decoration: none; display: flex; align-items: center; gap: 10px; padding: 10px 12px; border-radius: 8px; margin-top: 4px; }
        .core-nav a:hover, .core-nav a.active { color: #fff; background: rgba(255,255,255,.10); }
        .core-main { min-width: 0; }
        .core-topbar { background: #fff; border-bottom: 1px solid #e2e8f0; padding: 16px 28px; display: flex; justify-content: space-between; align-items: center; gap: 16px; }
        .core-content { padding: 28px; }
        .core-card { background: #fff; border: 1px solid #e2e8f0; border-radius: 8px; box-shadow: 0 12px 30px rgba(15, 23, 42, .05); }
        .stat-card { border-left: 4px solid var(--core-blue); }
        .btn-primary { background: var(--core-blue); border-color: var(--core-blue); }

        @media (max-width: 992px) {
            .core-shell { grid-template-columns: 1fr; }
            .core-sidebar { position: static; }
            .core-topbar { align-items: flex-start; flex-direction: column; }
        }
    </style>
</head>
<body>
<div class="core-shell">
    <aside class="core-sidebar">
        <a class="core-brand mb-4" href="<?= site_url('/dashboard') ?>">
            <img class="core-brand-logo" src="<?= base_url('assets/brand/one-core-favicon.png') ?>" alt="ONE CORE">
            <span>ONE CORE</span>
        </a>

        <div class="text-uppercase small text-white-50 mb-2">Noyau</div>
        <nav class="core-nav mb-4">
            <?php foreach ($coreMenu as $item): ?>
                <?php if (! $item['permission'] || $auth->can($item['permission'])): ?>
                    <a class="<?= $isActiveRoute($item['route']) ?>" href="<?= site_url($item['route']) ?>">
                        <i class="bi bi-<?= esc($item['icon']) ?>"></i>
                        <span><?= esc($item['label']) ?></span>
                    </a>
                <?php endif; ?>
            <?php endforeach; ?>
        </nav>

        <?php if ($moduleMenus !== []): ?>
            <div class="text-uppercase small text-white-50 mb-2">Modules</div>
            <nav class="core-nav">
                <?php foreach ($moduleMenus as $item): ?>
                    <a class="<?= $isActiveRoute($item['route']) ?>" href="<?= site_url($item['route']) ?>">
                        <i class="<?= esc($item['icon'] ?? 'bi bi-box') ?>"></i>
                        <span><?= esc($item['label']) ?></span>
                    </a>
                <?php endforeach; ?>
            </nav>
        <?php endif; ?>
    </aside>

    <main class="core-main">
        <header class="core-topbar">
            <div>
                <h1 class="h4 mb-0"><?= esc($title ?? 'ONE CORE') ?></h1>
                <div class="text-muted small">Socle modulaire pour applications professionnelles auto-hébergées</div>
            </div>
            <?php if ($user): ?>
                <div class="d-flex align-items-center gap-3">
                    <span class="small text-muted"><?= esc(trim(($user['firstname'] ?? '') . ' ' . ($user['lastname'] ?? '')) ?: ($user['email'] ?? '')) ?></span>
                    <a class="btn btn-outline-secondary btn-sm" href="<?= site_url('/logout') ?>">Déconnexion</a>
                </div>
            <?php else: ?>
                <a class="btn btn-outline-secondary btn-sm" href="<?= site_url('/login') ?>">Connexion Core</a>
            <?php endif; ?>
        </header>

        <section class="core-content">
            <?php if (session('success')): ?>
                <div class="alert alert-success"><?= esc(session('success')) ?></div>
            <?php endif; ?>
            <?php if (session('error')): ?>
                <div class="alert alert-danger"><?= esc(session('error')) ?></div>
            <?php endif; ?>

            <?= $this->renderSection('content') ?>
        </section>
    </main>
</div>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"></script>
<?= $this->renderSection('scripts') ?>
</body>
</html>
