@extends('layouts.app')

@section('page_title', 'Die Kaffeeliste - Verwaltung')

@php
    $data = $overview ?? [
        'metrics' => [],
        'tenants' => [],
        'shared_access' => [],
        'operations' => [],
        'roleOverview' => [
            'metrics' => [],
            'roles' => [],
            'delegation_rules' => [],
            'tenant_snapshots' => [],
            'notes' => [],
        ],
    ];
    $roleOverview = $roleOverview ?? ($data['roleOverview'] ?? [
        'metrics' => [],
        'roles' => [],
        'delegation_rules' => [],
        'tenant_snapshots' => [],
        'notes' => [],
    ]);
@endphp

@section('content')
    <section class="hero hero--split">
        <div class="hero__content">
            <div>
                <p class="hero__kicker">Verwaltung</p>
                <h2 class="hero__title">Mandanten</h2>
                <p class="hero__lead">
                    Diese Konsole ist die zentrale Schaltstelle für Tenant-Rollout, Domains, zentrale Anmeldung und den Umgang
                    mit Mitarbeitenden, die in mehreren Tenants hinterlegt sind. Statt verteilter Einzelansichten entsteht eine
                    gemeinsame Portfolio-Sicht für Betrieb und Weiterentwicklung.
                </p>
            </div>
            <div class="hero__meta">
                <span class="badge badge--solid">Zentrale Steuerung</span>
                <span class="badge">Anmeldung</span>
                <span class="badge">Identity & Routing</span>
                <span class="badge">Admin-Konsole</span>
            </div>
        </div>

        <aside class="hero__aside">
            @foreach ($data['metrics'] as $metric)
                <article class="card metric metric--compact">
                    <p class="metric__label">{{ $metric['label'] }}</p>
                    <div class="metric__value">{{ $metric['value'] }}</div>
                    <p class="muted">{{ $metric['detail'] }}</p>
                </article>
            @endforeach
        </aside>
    </section>

    <section class="table-card">
        <div class="table-card__header">
            <div>
                <p class="card__eyebrow">Portfolio</p>
                <h3>Tenant-Portfolio mit Betriebs- und Login-Sicht</h3>
            </div>
            <span class="pill">Zentrale Steuerung</span>
        </div>
        <div class="table-card__body">
            <table>
                <thead>
                    <tr>
                        <th>Mandant</th>
                        <th>Domain</th>
                        <th>Mitglieder</th>
                        <th>Login-Modell</th>
                        <th>SSO</th>
                        <th>Status</th>
                    </tr>
                </thead>
                <tbody>
                    @foreach ($data['tenants'] as $tenant)
                        <tr>
                            <td>
                                <strong>{{ $tenant['name'] }}</strong><br>
                                <span class="muted">{{ $tenant['tenant_key'] }} - {{ $tenant['primary_contact'] }}</span>
                            </td>
                            <td>{{ $tenant['domain'] }}</td>
                            <td>{{ $tenant['member_count'] }} Mitglieder / {{ $tenant['admin_count'] }} Admins</td>
                            <td>{{ $tenant['login_mode'] }}</td>
                            <td>{{ $tenant['oidc_provider_count'] }} Provider</td>
                            <td>
                                @if ($tenant['status'] === 'active')
                                    <span class="status">Aktiv</span>
                                @else
                                    <span class="status status--warning">Sandbox</span>
                                @endif
                            </td>
                        </tr>
                    @endforeach
                </tbody>
            </table>
        </div>
    </section>

    <section class="split" style="margin-top: 18px;">
        <article class="panel">
            <p class="card__eyebrow">Mehrfachzuordnungen</p>
            <h3>Wie die Plattform E-Mail-Adressen über mehrere Tenants hinweg behandelt.</h3>
            <div class="timeline" style="margin-top: 18px;">
                @foreach ($data['shared_access'] as $entry)
                    <div class="timeline__item">
                        <p class="timeline__title">{{ $entry['email'] }}</p>
                        <p class="timeline__meta">
                            @if ($entry['tenants'] !== [])
                                {{ implode(', ', $entry['tenants']) }}
                            @else
                                Keine Tenant-Zuordnung vorhanden
                            @endif
                        </p>
                        <div class="toolbar" style="margin-top: 2px;">
                            @if ($entry['status'] === 'einzeln')
                                <span class="status">Direkte Weiterleitung</span>
                            @elseif ($entry['status'] === 'mehrfach')
                                <span class="status status--warning">Tenant-Auswahl</span>
                            @else
                                <span class="status status--danger">Einladung nötig</span>
                            @endif
                        </div>
                        <p class="muted">{{ $entry['next_step'] }}</p>
                    </div>
                @endforeach
            </div>
        </article>

        <article class="panel">
            <p class="card__eyebrow">Zentrale Aufgaben</p>
            <h3>Prioritäten für den Plattformbetrieb.</h3>
            <ul class="list-reset" style="margin-top: 18px;">
                @foreach ($data['operations'] as $operation)
                    <li>
                        <span class="status">{{ $operation['state'] }}</span>
                        <strong style="display: block; margin-top: 8px;">{{ $operation['title'] }}</strong>
                        <span class="muted">{{ $operation['detail'] }}</span>
                    </li>
                @endforeach
            </ul>
            <div class="note" style="margin-top: 18px;">
                Die Tenant Console ist bewusst nicht nur eine Tabelle: Sie verbindet Rollout, Login-Logik, Mehrfachzuordnungen
                und zentrale Operations-Signale zu einer konsistenten Admin-Oberfläche.
            </div>
        </article>
    </section>

    <section class="table-card" style="margin-top: 18px;">
        <div class="table-card__header">
            <div>
                <p class="card__eyebrow">Rollenmodell</p>
                <h3>Tenant-Admin Vollzugriff plus Spezialrollen mit klarer Delegation.</h3>
            </div>
            <span class="pill">Im Menü unter Rollen</span>
        </div>
        <div class="grid grid--4" style="margin-bottom: 18px;">
            @foreach ($roleOverview['metrics'] as $metric)
                <article class="metric metric--compact">
                    <p class="metric__label">{{ $metric['label'] }}</p>
                    <div class="metric__value">{{ $metric['value'] }}</div>
                    <p class="muted">{{ $metric['detail'] }}</p>
                </article>
            @endforeach
        </div>
        <div class="table-card__body">
            <table>
                <thead>
                    <tr>
                        <th>Rolle</th>
                        <th>Bereich</th>
                        <th>Rechte</th>
                        <th>Delegation</th>
                    </tr>
                </thead>
                <tbody>
                    @foreach ($roleOverview['roles'] as $role)
                        <tr>
                            <td>
                                <strong>{{ $role['label'] }}</strong><br>
                                <span class="muted">{{ $role['key'] }}</span>
                            </td>
                            <td>{{ $role['scope'] }}</td>
                            <td>{{ implode(', ', $role['rights']) }}</td>
                            <td>
                                {{ $role['delegation'] }}<br>
                                <span class="muted">{{ $role['notes'] }}</span>
                            </td>
                        </tr>
                    @endforeach
                </tbody>
            </table>
        </div>
    </section>
@endsection
