<?php
function sd_h($v) {
    return htmlspecialchars((string)$v, ENT_QUOTES, 'UTF-8');
}
?>

<div class="container-fluid py-3">
    <div class="card border-0 shadow-sm mb-4" style="border-radius:24px;background:linear-gradient(135deg,#0f172a,#2563eb);color:white;">
        <div class="card-body p-4">
            <h3 class="fw-bold mb-1">
                <i class="fa fa-bug"></i>
                Scope Debug
            </h3>
            <div class="opacity-75">Current login role, conference, organization, and landing URL.</div>
        </div>
    </div>

    <div class="row g-4">
        <div class="col-lg-4">
            <div class="card border-0 shadow-sm h-100" style="border-radius:22px;">
                <div class="card-body p-4">
                    <h5 class="fw-bold">User</h5>
                    <table class="table table-bordered">
                        <tr><th>ID</th><td><?= sd_h($user['id'] ?? '') ?></td></tr>
                        <tr><th>Email</th><td><?= sd_h($user['email'] ?? $user['username'] ?? '') ?></td></tr>
                        <tr><th>DB Role</th><td><?= sd_h($user['role'] ?? $user['role_code'] ?? $user['user_role'] ?? '') ?></td></tr>
                        <tr><th>DB Org</th><td><?= sd_h($user['org_id'] ?? $user['organization_id'] ?? '') ?></td></tr>
                    </table>
                </div>
            </div>
        </div>

        <div class="col-lg-4">
            <div class="card border-0 shadow-sm h-100" style="border-radius:22px;">
                <div class="card-body p-4">
                    <h5 class="fw-bold">Resolved Scope</h5>
                    <table class="table table-bordered">
                        <tr><th>Role</th><td><span class="badge bg-primary"><?= sd_h($session['conference_role'] ?? '') ?></span></td></tr>
                        <tr><th>Org ID</th><td><?= sd_h($session['org_id'] ?? '') ?></td></tr>
                        <tr><th>Conference ID</th><td><?= sd_h($session['conference_id'] ?? '') ?></td></tr>
                        <tr><th>Conference</th><td><?= sd_h($conference['short_name'] ?? '') ?></td></tr>
                        <tr><th>Landing</th><td><code><?= sd_h($landing) ?></code></td></tr>
                    </table>
                </div>
            </div>
        </div>

        <div class="col-lg-4">
            <div class="card border-0 shadow-sm h-100" style="border-radius:22px;">
                <div class="card-body p-4">
                    <h5 class="fw-bold">Available Conferences</h5>
                    <?php foreach ($available as $c): ?>
                        <div class="border rounded-3 p-2 mb-2">
                            <strong>#<?= (int)$c['id'] ?> <?= sd_h($c['short_name'] ?? '') ?></strong>
                            <div class="text-muted small">Org <?= (int)($c['org_id'] ?? 0) ?></div>
                        </div>
                    <?php endforeach; ?>
                    <?php if (!$available): ?>
                        <div class="text-muted">No conferences available.</div>
                    <?php endif; ?>
                </div>
            </div>
        </div>
    </div>

    <details class="mt-4">
        <summary class="fw-bold">Raw Session</summary>
        <pre class="bg-dark text-white p-3 rounded mt-2" style="max-height:420px;overflow:auto;"><?= sd_h(print_r($session, true)) ?></pre>
    </details>
</div>
