<?php

use PpPhp\Core\Assets\SpaAssetManager;
// Required variables: $baseUrl, $assetsUrl, $project, $user, $navItems, $innerContent
?>
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title><?= htmlspecialchars($project['name'] ?? 'Project') ?> · PPPHP</title>
    <?php include constant('BASE_PATH') . '/core/views/favicon.php'; ?>
    <link href="<?= $assetsUrl ?>/bootstrap/css/bootstrap.min.css" rel="stylesheet">
    <link href="<?= $assetsUrl ?>/icons/bootstrap-icons.css" rel="stylesheet">
    <?php include constant('BASE_PATH') . '/core/views/theme-head.php'; ?>
    <link href="<?= $assetsUrl ?>/css/project-shell.css?v=<?= (int) @filemtime(constant('BASE_PATH') . '/public/assets/css/project-shell.css') ?>" rel="stylesheet">
    <link href="<?= $assetsUrl ?>/css/workspace.css?v=<?= (int) @filemtime(constant('BASE_PATH') . '/public/assets/css/workspace.css') ?>" rel="stylesheet">
    <link href="<?= $assetsUrl ?>/css/project-shell-mobile.css?v=<?= (int) @filemtime(constant('BASE_PATH') . '/public/assets/css/project-shell-mobile.css') ?>" rel="stylesheet">
    <link href="<?= $assetsUrl ?>/sweetalert2/sweetalert2.min.css" rel="stylesheet">
    <style><?php readfile(constant('BASE_PATH') . '/modules/Projects/Views/styles/databaseConnection.css'); ?></style>
    <style><?php readfile(constant('BASE_PATH') . '/modules/Projects/Views/styles/projectsTheme.css'); ?></style>
    <?= \PpPhp\Core\ModuleAssets::style('ProjectWorkspace', 'Views/styles/workspaceTheme.css') ?>

    <?= SpaAssetManager::renderIconCss($baseUrl) ?>
</head>

<body>

    <!-- Top Navbar (with integrated project toolbar – same as Tables shell) -->
    <?php include constant('BASE_PATH') . '/core/views/project-navbar.php'; ?>

    <!-- Sidebar Desktop -->
    <nav id="sidebarDesktop" class="sidebar d-none d-lg-flex flex-column py-3">
        <ul class="nav nav-pills flex-column mb-auto">
            <li class="nav-item">
                <a href="<?= $baseUrl ?>/projects" class="nav-link">
                    <i class="bi bi-arrow-left me-2"></i> Back to Projects
                </a>
            </li>
            <li>
                <hr class="text-secondary my-2">
            </li>
            <?php foreach ($navItems as $item): ?>
                <li class="nav-item">
                    <a href="<?= $baseUrl ?>/project/<?= urlencode($project['slug']) ?><?= htmlspecialchars($item['uri'] ?? '#') ?>"
                        class="nav-link <?= ($item['active'] ?? false) ? 'active' : '' ?>">
                        <i class="bi bi-<?= $item['icon'] ?? 'circle' ?> me-2"></i>
                        <?= htmlspecialchars($item['label']) ?>
                    </a>
                </li>
            <?php endforeach; ?>
        </ul>
        <div class="mt-auto px-3 pb-3"><small class="text-muted">v1.0</small></div>
    </nav>

    <!-- Mobile Offcanvas -->
    <div class="offcanvas offcanvas-start bg-dark text-white d-lg-none" id="sidebarOffcanvas">
        <div class="offcanvas-header">
            <h5 class="offcanvas-title"><?= htmlspecialchars($project['name'] ?? 'Project') ?></h5>
            <button type="button" class="btn-close btn-close-white" data-bs-dismiss="offcanvas" aria-label="Close navigation"></button>
        </div>
        <div class="offcanvas-body">
            <a href="<?= $baseUrl ?>/projects" class="btn btn-outline-light w-100 mb-3"><i class="bi bi-arrow-left me-2"></i> Back to Projects</a>
            <ul class="nav nav-pills flex-column">
                <?php foreach ($navItems as $item): ?>
                    <li class="nav-item">
                        <a href="<?= $baseUrl ?>/project/<?= urlencode($project['slug']) ?><?= htmlspecialchars($item['uri'] ?? '#') ?>" class="nav-link text-white">
                            <i class="bi bi-<?= $item['icon'] ?? 'circle' ?> me-2"></i><?= htmlspecialchars($item['label']) ?>
                        </a>
                    </li>
                <?php endforeach; ?>
            </ul>
        </div>
    </div>

    <!-- Main Content -->
    <main id="mainArea" class="main-area workspace-main">
        <div class="workspace-scroll">
            <?= $innerContent ?>
        </div>
    </main>

    <?php include constant('BASE_PATH') . '/modules/Projects/Views/partials/database_connection_modal.php'; ?>

    <script src="<?= $assetsUrl ?>/bootstrap/js/bootstrap.bundle.min.js"></script>
    <script src="<?= $assetsUrl ?>/sweetalert2/sweetalert2.min.js"></script>
    <?php if (!empty($resumePrompt)): ?>
    <script>window.PP_RESUME = <?= json_encode($resumePrompt, JSON_UNESCAPED_SLASHES) ?>;</script>
    <?php endif; ?>
    <script src="<?= $assetsUrl ?>/js/project-shell.js?v=<?= (int) @filemtime((defined('BASE_PATH') ? constant('BASE_PATH') : dirname(__DIR__, 3)) . '/public/assets/js/project-shell.js') ?>"></script>
    <script>
        document.querySelectorAll('[data-bs-toggle="tooltip"]').forEach((element) => {
            bootstrap.Tooltip.getOrCreateInstance(element);
        });
    </script>
    <script>
        const baseUrl = <?= json_encode($baseUrl) ?>;
        const projectSlug = <?= json_encode($project['slug'] ?? '') ?>;
        window.projectsBaseUrl = baseUrl;
        window.projectDatabaseDefinitions = <?= json_encode($databaseTypes, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE) ?>;
    </script>
    <script><?php readfile(constant('BASE_PATH') . '/modules/Projects/Views/scripts/databaseConnection.js'); ?></script>

    <?= SpaAssetManager::renderSharedIconManagerJs($baseUrl) ?>
    <?= SpaAssetManager::renderIconJs($baseUrl) ?>
</body>

</html>
