<?php
$displayName = trim((string)($user['fullname'] ?? $user['username'] ?? 'Developer'));
$projectName = (string)($project['name'] ?? 'Your project');
$projectDescription = trim((string)($project['description'] ?? ''));
$projectDescription = $projectDescription !== ''
    ? $projectDescription
    : 'Turn your data model into a secure, responsive web application—without rebuilding the same foundation every time.';
$projectBase = $baseUrl . '/project/' . rawurlencode((string)$project['slug']);
$sa = !empty($standalone);
?>
<div class="workspace-welcome">
    <?php if (!empty($projectFlash)): ?>
        <div class="workspace-flash" role="status">
            <i class="bi bi-info-circle-fill"></i>
            <span><?= htmlspecialchars((string)$projectFlash) ?></span>
            <button type="button" class="workspace-flash-x" aria-label="Dismiss" data-flash-dismiss>&times;</button>
        </div>
    <?php endif; ?>
    <section class="workspace-hero">
        <div class="hero-glow hero-glow-one"></div>
        <div class="hero-glow hero-glow-two"></div>
        <div class="hero-content">
            <div class="welcome-kicker"><i class="bi bi-stars"></i> Welcome back, <?= htmlspecialchars($displayName) ?><?php if ($sa): ?> <span class="welcome-sa-tag"><i class="bi bi-window-stack"></i> Standalone app</span><?php endif; ?></div>
            <h1>Build <span><?= htmlspecialchars($projectName) ?></span> with clarity.</h1>
            <p><?= $sa ? 'A database-free app — design its pages in the Designer, style it, and build. Tables, Query, Pages, Fields and Security are off for this project.' : htmlspecialchars($projectDescription) ?></p>
            <div class="hero-actions">
                <?php if ($sa): ?>
                    <a class="btn btn-light btn-sm px-3" href="<?= $projectBase ?>/designer"><i class="bi bi-palette me-1"></i> Open Designer</a>
                    <a class="btn btn-outline-light btn-sm px-3" href="<?= $projectBase ?>/finished"><i class="bi bi-rocket-takeoff me-1"></i> Build app</a>
                <?php else: ?>
                    <a class="btn btn-light btn-sm px-3" href="<?= $projectBase ?>/tables"><i class="bi bi-table me-1"></i> Start with data</a>
                    <a class="btn btn-outline-light btn-sm px-3" href="<?= $projectBase ?>/designer"><i class="bi bi-palette me-1"></i> Open Designer</a>
                <?php endif; ?>
            </div>
        </div>
        <?php if ($sa): ?>
        <div class="hero-status hero-status-static">
            <span class="progress-ring" style="--progress-angle:<?= ((int)$workspaceStats['progress'] * 3.6) ?>deg">
                <span class="progress-ring-content"><strong><?= (int)$workspaceStats['progress'] ?>%</strong><small>ready</small></span>
            </span>
            <span class="database-pill">
                <span class="database-dot"></span>
                <span class="database-pill-copy"><small>App type</small><strong>Standalone</strong></span>
            </span>
            <span class="database-target"><i class="bi bi-window-stack"></i> No database</span>
        </div>
        <?php else: ?>
        <button type="button" class="hero-status" data-edit-database
            data-project-slug="<?= htmlspecialchars((string)$project['slug']) ?>"
            data-project-name="<?= htmlspecialchars($projectName) ?>"
            data-bs-toggle="tooltip"
            data-bs-placement="top"
            data-bs-custom-class="workspace-tooltip"
            title="Edit, test, or switch this project's database connection"
            aria-label="Manage the <?= htmlspecialchars($workspaceStats['databaseLabel']) ?> database connection for <?= htmlspecialchars($projectName) ?>">
            <span class="progress-ring" style="--progress-angle:<?= ((int)$workspaceStats['progress'] * 3.6) ?>deg">
                <span class="progress-ring-content"><strong><?= (int)$workspaceStats['progress'] ?>%</strong><small>setup</small></span>
            </span>
            <span class="database-pill">
                <span class="database-dot"></span>
                <span class="database-pill-copy"><small>Connected with</small><strong data-project-db-label><?= htmlspecialchars($workspaceStats['databaseLabel']) ?></strong></span>
            </span>
            <?php if ($workspaceStats['databaseTarget'] !== ''): ?>
                <span class="database-target"><i class="bi bi-database-check"></i> <span data-project-db-target><?= htmlspecialchars($workspaceStats['databaseTarget']) ?></span></span>
            <?php endif; ?>
            <span class="database-manage-hint"><i class="bi bi-gear"></i> Manage connection</span>
        </button>
        <?php endif; ?>
    </section>

    <section class="workspace-stats" aria-label="Project status">
        <?php if ($sa): ?>
        <div class="stat-item violet"><span class="stat-icon violet"><i class="bi bi-window-stack"></i></span><div><strong>Pages</strong><small>designed in the app</small></div></div>
        <div class="stat-item blue"><span class="stat-icon blue"><i class="bi bi-database-slash"></i></span><div><strong>None</strong><small>database required</small></div></div>
        <div class="stat-item pink"><span class="stat-icon pink"><i class="bi bi-globe2"></i></span><div><strong>Public</strong><small>no login needed</small></div></div>
        <div class="stat-item green"><span class="stat-icon green"><i class="bi bi-lightning-charge"></i></span><div><strong>1-click</strong><small>application build</small></div></div>
        <?php else: ?>
        <div class="stat-item blue"><span class="stat-icon blue"><i class="bi bi-table"></i></span><div><strong><?= (int)$workspaceStats['tables'] ?></strong><small>project tables</small></div></div>
        <div class="stat-item violet"><span class="stat-icon violet"><i class="bi bi-file-earmark"></i></span><div><strong><?= (int)$workspaceStats['pageTables'] ?></strong><small>page sets</small></div></div>
        <div class="stat-item pink"><span class="stat-icon pink"><i class="bi bi-palette2"></i></span><div><strong><?= (int)$workspaceStats['designs'] ?></strong><small>designed tables</small></div></div>
        <div class="stat-item green"><span class="stat-icon green"><i class="bi bi-lightning-charge"></i></span><div><strong>1-click</strong><small>application build</small></div></div>
        <?php endif; ?>
    </section>

    <?php if ($sa): ?>
    <section class="workspace-section">
        <div class="section-heading">
            <div><span>Standalone toolkit</span><h2>Design pages, style them, and ship — no database needed</h2></div>
            <a href="<?= $projectBase ?>/designer">Open Designer <i class="bi bi-arrow-right"></i></a>
        </div>
        <div class="capability-grid">
            <a class="capability-card pages" href="<?= $projectBase ?>/designer">
                <span class="capability-icon"><i class="bi bi-palette"></i></span>
                <div><h3>Design your pages</h3><p>Build standalone &amp; common pages from components — hero sections, cards, charts, media and more.</p></div>
                <i class="bi bi-arrow-up-right"></i>
            </a>
            <a class="capability-card data" href="<?= $projectBase ?>/style">
                <span class="capability-icon"><i class="bi bi-brush"></i></span>
                <div><h3>Style &amp; theme</h3><p>Pick a theme, tune colours and typography, and set the navigation and menu.</p></div>
                <i class="bi bi-arrow-up-right"></i>
            </a>
            <a class="capability-card logic" href="<?= $projectBase ?>/custom-files">
                <span class="capability-icon"><i class="bi bi-folder2-open"></i></span>
                <div><h3>Add your own files</h3><p>Drop in custom assets, scripts or pages that ship alongside the generated app.</p></div>
                <i class="bi bi-arrow-up-right"></i>
            </a>
            <a class="capability-card ship" href="<?= $projectBase ?>/finished">
                <span class="capability-icon"><i class="bi bi-rocket-takeoff"></i></span>
                <div><h3>Build and ship</h3><p>Generate a complete, database-free PHP app and deploy it anywhere.</p></div>
                <i class="bi bi-arrow-up-right"></i>
            </a>
        </div>
    </section>

    <section class="build-path">
        <div class="path-title"><i class="bi bi-signpost-split"></i><div><strong>A simple path to a live page</strong><small>No database — everything is designed and built from here.</small></div></div>
        <div class="path-steps">
            <a href="<?= $projectBase ?>/designer"><b>1</b><span>Design pages</span></a>
            <i class="bi bi-chevron-right"></i>
            <a href="<?= $projectBase ?>/style"><b>2</b><span>Style app</span></a>
            <i class="bi bi-chevron-right"></i>
            <a href="<?= $projectBase ?>/finished"><b>3</b><span>Build app</span></a>
        </div>
    </section>
    <?php else: ?>
    <section class="workspace-section">
        <div class="section-heading">
            <div><span>Your RAD toolkit</span><h2>Everything needed to go from database to application</h2></div>
            <a href="<?= $projectBase ?>/tables">Continue building <i class="bi bi-arrow-right"></i></a>
        </div>
        <div class="capability-grid">
            <a class="capability-card data" href="<?= $projectBase ?>/tables">
                <span class="capability-icon"><i class="bi bi-diagram-3"></i></span>
                <div><h3>Model your data</h3><p>Create tables and views, connect relationships, and keep the schema visual.</p></div>
                <i class="bi bi-arrow-up-right"></i>
            </a>
            <a class="capability-card pages" href="<?= $projectBase ?>/pages">
                <span class="capability-icon"><i class="bi bi-window-stack"></i></span>
                <div><h3>Shape the experience</h3><p>Choose pages and fields, then compose responsive layouts in Designer.</p></div>
                <i class="bi bi-arrow-up-right"></i>
            </a>
            <a class="capability-card logic" href="<?= $projectBase ?>/events">
                <span class="capability-icon"><i class="bi bi-braces-asterisk"></i></span>
                <div><h3>Add logic safely</h3><p>Use events, queries, permissions, and security rules without losing control.</p></div>
                <i class="bi bi-arrow-up-right"></i>
            </a>
            <a class="capability-card ship" href="<?= $projectBase ?>/finished">
                <span class="capability-icon"><i class="bi bi-rocket-takeoff"></i></span>
                <div><h3>Build and ship</h3><p>Generate a complete PHP application, review readiness, and deploy.</p></div>
                <i class="bi bi-arrow-up-right"></i>
            </a>
        </div>
    </section>

    <section class="build-path">
        <div class="path-title"><i class="bi bi-signpost-split"></i><div><strong>A simple path to production</strong><small>Move left to right; return to any step whenever the project evolves.</small></div></div>
        <div class="path-steps">
            <a href="<?= $projectBase ?>/tables"><b>1</b><span>Connect data</span></a>
            <i class="bi bi-chevron-right"></i>
            <a href="<?= $projectBase ?>/pages"><b>2</b><span>Choose pages</span></a>
            <i class="bi bi-chevron-right"></i>
            <a href="<?= $projectBase ?>/designer"><b>3</b><span>Design UI</span></a>
            <i class="bi bi-chevron-right"></i>
            <a href="<?= $projectBase ?>/finished"><b>4</b><span>Build app</span></a>
        </div>
    </section>
    <?php endif; ?>
</div>
