<?php
/** @var array<string, mixed> $pageData */
?>
<!DOCTYPE html>
<html lang="de">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Kaffeeliste SaaS - Tenant Rollen</title>
    <style>
        :root{
            --bg:#f4efe6;
            --card:#fffaf3;
            --ink:#22160f;
            --muted:#675546;
            --brand:#0c6b66;
            --brand-2:#084d49;
            --accent:#a34b12;
            --line:rgba(34,22,15,.12);
            --shadow:0 24px 54px rgba(57,35,22,.11);
            --radius:24px;
        }
        *{box-sizing:border-box}
        body{
            margin:0;
            min-height:100vh;
            font-family:"Aptos","Segoe UI",sans-serif;
            color:var(--ink);
            background:
                radial-gradient(circle at top left, rgba(163,75,18,.10), transparent 26%),
                radial-gradient(circle at top right, rgba(12,107,102,.14), transparent 24%),
                linear-gradient(180deg,#fbf8f2 0%,var(--bg) 100%);
        }
        a{color:inherit;text-decoration:none}
        h1,h2,h3{font-family:Georgia,serif;letter-spacing:-.02em}
        .shell{width:min(1300px,calc(100vw - 32px));margin:24px auto 40px}
        .bar,.hero,.card,.table-card,.note{border:1px solid var(--line);border-radius:var(--radius);background:var(--card);box-shadow:var(--shadow)}
        .bar{display:flex;justify-content:space-between;align-items:center;gap:16px;padding:18px 22px;margin-bottom:18px;flex-wrap:wrap}
        .brand{display:grid;gap:4px}
        .brand strong{font-size:1.26rem}
        .brand span,.muted{color:var(--muted)}
        .toolbar,.actions,.context,.meta{display:flex;flex-wrap:wrap;gap:10px;align-items:center}
        .badge,.pill{display:inline-flex;align-items:center;padding:7px 12px;border-radius:999px;background:rgba(12,107,102,.08);color:var(--brand);font-weight:700;font-size:.86rem}
        .badge--solid{background:linear-gradient(135deg,var(--brand),var(--brand-2));color:#fff}
        .hero{display:grid;grid-template-columns:minmax(0,1.4fr) minmax(260px,.6fr);gap:20px;padding:28px;margin-bottom:18px}
        .hero__kicker{margin:0 0 10px;color:var(--accent);text-transform:uppercase;letter-spacing:.15em;font-size:.8rem;font-weight:800}
        .hero__title{margin:0 0 12px;font-size:clamp(2rem,4vw,3.5rem);line-height:1.05}
        .hero__lead{margin:0;color:var(--muted);max-width:70ch;line-height:1.7}
        .hero__actions{display:flex;flex-wrap:wrap;gap:12px;margin-top:16px}
        .button{display:inline-flex;align-items:center;justify-content:center;padding:11px 16px;border-radius:999px;border:0;background:linear-gradient(135deg,var(--brand),var(--brand-2));color:#fff;font-weight:700}
        .button--ghost{background:transparent;color:var(--brand);border:1px solid rgba(12,107,102,.18)}
        .grid{display:grid;gap:18px}
        .grid--2{grid-template-columns:repeat(2,minmax(0,1fr))}
        .grid--3{grid-template-columns:repeat(3,minmax(0,1fr))}
        .grid--4{grid-template-columns:repeat(4,minmax(0,1fr))}
        .metric{padding:18px;border:1px solid var(--line);border-radius:20px;background:#fffdf8}
        .metric__label{color:var(--muted)}
        .metric__value{font-size:1.9rem;font-weight:800;margin:6px 0 8px}
        .table-card,.card{padding:22px}
        .table-card__header{display:flex;justify-content:space-between;gap:12px;align-items:flex-start;margin-bottom:16px;flex-wrap:wrap}
        table{width:100%;border-collapse:collapse}
        th,td{padding:12px 10px;border-bottom:1px solid var(--line);text-align:left;vertical-align:top}
        th{font-size:.82rem;text-transform:uppercase;letter-spacing:.08em;color:var(--muted)}
        .status{display:inline-flex;align-items:center;padding:6px 10px;border-radius:999px;background:rgba(12,107,102,.1);color:var(--brand);font-weight:700;font-size:.84rem}
        .status--warning{background:rgba(163,75,18,.12);color:#98510c}
        .status--success{background:rgba(17,98,61,.12);color:#11623d}
        .list-reset{margin:0;padding-left:18px}
        .list-reset li+li{margin-top:10px}
        .note{padding:16px 18px;background:rgba(12,107,102,.06)}
        .stack{display:grid;gap:12px}
        .chip-list{display:flex;flex-wrap:wrap;gap:8px}
        .chip{display:inline-flex;align-items:center;padding:5px 10px;border-radius:999px;background:rgba(12,107,102,.08);color:var(--brand);font-size:.84rem;font-weight:700}
        @media(max-width:960px){.hero,.grid--2,.grid--3,.grid--4{grid-template-columns:1fr}.bar{align-items:flex-start;flex-direction:column}}
    </style>
</head>
<body>
    <main class="shell">
        <header class="bar">
            <div class="brand">
                <strong>Kaffeeliste SaaS</strong>
                <span>Tenant Rollen, Rechte und Delegation an einem Ort.</span>
            </div>
            <div class="toolbar">
                <span class="badge"><?= tenant_roles_h((string) ($tenant['name'] ?? 'Tenant')) ?></span>
                <span class="badge">Tenant-Admin Vollzugriff</span>
                <span class="badge badge--solid">Vier-Augen optional</span>
            </div>
        </header>

        <section class="hero">
            <div>
                <p class="hero__kicker">Tenant Rollenmodell</p>
                <h1 class="hero__title">Die Plattform zeigt klar, wer was darf und wer es weitergeben kann.</h1>
                <p class="hero__lead">
                    Tenant-Admins behalten den Gesamtzugriff, während finance_admin, support_contact und survey_manager
                    als Fachrollen gezielt delegiert werden. Wenn eine Funktion noch nicht direkt in der Anwendung steckt,
                    wird sie als Tenant-Funktion eingerichtet und sichtbar gemacht.
                </p>
                <div class="hero__actions">
                    <a class="button" href="/tenants/">Zur Tenant-Konsole</a>
                    <a class="button button--ghost" href="/dashboard/">Zum Tenant-Dashboard</a>
                </div>
                <div class="meta" style="margin-top:16px;">
                    <span class="badge">lokal + ADFS/OIDC</span>
                    <span class="badge">Rollenmatrix</span>
                    <span class="badge">Delegation</span>
                </div>
            </div>

            <div class="stack">
                <?php foreach (array_slice($metrics, 0, 4) as $metric): ?>
                    <article class="metric">
                        <div class="metric__label"><?= tenant_roles_h((string) ($metric['label'] ?? 'Kennzahl')) ?></div>
                        <div class="metric__value"><?= tenant_roles_h((string) ($metric['value'] ?? '-')) ?></div>
                        <div class="muted"><?= tenant_roles_h((string) ($metric['detail'] ?? '')) ?></div>
                    </article>
                <?php endforeach; ?>
            </div>
        </section>

        <section class="grid grid--2">
            <article class="table-card">
                <div class="table-card__header">
                    <div>
                        <p class="hero__kicker">Rollenmatrix</p>
                        <h2>Vollzugriff, Spezialrollen und Leserechte</h2>
                    </div>
                    <span class="pill">tenant scoped</span>
                </div>
                <table>
                    <thead>
                        <tr>
                            <th>Rolle</th>
                            <th>Rechte</th>
                            <th>Delegation</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php foreach ($roles as $role): ?>
                            <tr>
                                <td>
                                    <strong><?= tenant_roles_h((string) ($role['label'] ?? $role['key'] ?? 'Rolle')) ?></strong><br>
                                    <span class="muted"><?= tenant_roles_h((string) ($role['scope'] ?? '')) ?></span>
                                </td>
                                <td>
                                    <div class="chip-list">
                                        <?php foreach (($role['rights'] ?? []) as $right): ?>
                                            <span class="chip"><?= tenant_roles_h((string) $right) ?></span>
                                        <?php endforeach; ?>
                                    </div>
                                </td>
                                <td>
                                    <?= tenant_roles_h((string) ($role['delegation'] ?? '')) ?><br>
                                    <span class="muted"><?= tenant_roles_h((string) ($role['notes'] ?? '')) ?></span>
                                </td>
                            </tr>
                        <?php endforeach; ?>
                    </tbody>
                </table>
            </article>

            <article class="card">
                <p class="hero__kicker">Delegation</p>
                <h2>Wer Rollen vergeben darf</h2>
                <div class="stack">
                    <?php foreach ($delegationRules as $rule): ?>
                        <div class="note">
                            <strong><?= tenant_roles_h((string) ($rule['from'] ?? '')) ?> → <?= tenant_roles_h((string) ($rule['to'] ?? '')) ?></strong>
                            <div><?= tenant_roles_h((string) ($rule['rule'] ?? '')) ?></div>
                            <div class="muted" style="margin-top:4px;">
                                Freigabe: <?= tenant_roles_h((string) ($rule['approval'] ?? '')) ?>
                            </div>
                            <div class="muted"><?= tenant_roles_h((string) ($rule['note'] ?? '')) ?></div>
                        </div>
                    <?php endforeach; ?>
                </div>
                <div class="note" style="margin-top:16px;">
                    Das Vier-Augen-Prinzip kann tenantweit aktiviert oder deaktiviert werden. Kritische Aktionen werden
                    dadurch kontrolliert, ohne die Facharbeit in jedem Tenant gleich stark zu bremsen.
                </div>
            </article>
        </section>

        <section class="grid grid--2" style="margin-top:18px;">
            <article class="table-card">
                <div class="table-card__header">
                    <div>
                        <p class="hero__kicker">Tenant-Snapshots</p>
                        <h2>Wie die Rollen in den Tenants aussehen</h2>
                    </div>
                    <span class="pill"><?= tenant_roles_h((string) ($tenant['tenant_key'] ?? 'berlin')) ?></span>
                </div>
                <table>
                    <thead>
                        <tr>
                            <th>Mandant</th>
                            <th>Admins</th>
                            <th>Spezialrollen</th>
                            <th>Status</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php foreach ($tenantSnapshots as $snapshot): ?>
                            <tr>
                                <td>
                                    <strong><?= tenant_roles_h((string) ($snapshot['name'] ?? 'Tenant')) ?></strong><br>
                                    <span class="muted"><?= tenant_roles_h((string) ($snapshot['tenant_key'] ?? '')) ?> · <?= tenant_roles_h((string) ($snapshot['primary_contact'] ?? '')) ?></span>
                                </td>
                                <td><?= (int) ($snapshot['admin_count'] ?? 0) ?></td>
                                <td><?= tenant_roles_implode((array) ($snapshot['special_roles'] ?? [])) ?></td>
                                <td>
                                    <?php if (($snapshot['status'] ?? 'active') === 'active'): ?>
                                        <span class="status status--success"><?= tenant_roles_h((string) ($snapshot['delegation_state'] ?? 'Aktiv')) ?></span>
                                    <?php else: ?>
                                        <span class="status status--warning"><?= tenant_roles_h((string) ($snapshot['delegation_state'] ?? 'Prüfen')) ?></span>
                                    <?php endif; ?>
                                </td>
                            </tr>
                        <?php endforeach; ?>
                    </tbody>
                </table>
            </article>

            <article class="card">
                <p class="hero__kicker">Lesbare Sicht</p>
                <h2>Was Tenant-Admins direkt verstehen sollen</h2>
                <div class="stack">
                    <?php foreach ($permissionGroups as $group): ?>
                        <div class="note">
                            <strong><?= tenant_roles_h((string) ($group['title'] ?? 'Rechtegruppe')) ?></strong>
                            <div class="chip-list" style="margin-top:8px;">
                                <?php foreach (($group['items'] ?? []) as $item): ?>
                                    <span class="chip"><?= tenant_roles_h((string) $item) ?></span>
                                <?php endforeach; ?>
                            </div>
                        </div>
                    <?php endforeach; ?>
                </div>
                <ul class="list-reset" style="margin-top:16px;">
                    <?php foreach ($notes as $note): ?>
                        <li><?= tenant_roles_h((string) $note) ?></li>
                    <?php endforeach; ?>
                </ul>
            </article>
        </section>
    </main>
</body>
</html>
