@extends('layouts.app')

@section('page_title', $title ?? 'Die Kaffeeliste - Umfragen')

@php
    $board = $board ?? [
        'metrics' => [],
        'draft_surveys' => [],
        'published_snapshots' => [],
        'workflow' => [],
        'roles' => [],
        'publishing_rules' => [],
    ];
    $memberBoard = $memberBoard ?? ['snapshots' => [], 'participation' => []];
@endphp

@section('content')
    <section class="hero hero--split">
        <div class="hero__content">
            <div>
                <p class="hero__kicker">Umfragen</p>
                <h2 class="hero__title">Umfragen</h2>
                <p class="hero__lead">
                    Das Survey-Modul ist jetzt nicht mehr nur eine Zusatzfunktion, sondern ein klarer
                    Verwaltungsbereich für Tenant-Admins und Survey-Manager. Entwürfe werden live gepflegt,
                    Mitglieder sehen nach Freigabe einen stabilen Snapshot.
                </p>
            </div>
            <div class="hero__meta">
                <span class="badge">Administration</span>
                <span class="badge">Mitglieder-Sicht</span>
                <span class="badge">Tenant-weit</span>
                <span class="badge badge--solid">Freigabe gesteuert</span>
            </div>
        </div>

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

    <section class="grid grid--2">
        <article class="table-card">
            <div class="table-card__header">
                <div>
                    <p class="card__eyebrow">Administration</p>
                    <h3>Aktive Entwürfe und Freigabestufen</h3>
                </div>
                <span class="pill">Draft -> Review -> Snapshot</span>
            </div>
            <div class="table-card__body">
                <table>
                    <thead>
                        <tr>
                            <th>Umfrage</th>
                            <th>Fragen</th>
                            <th>Status</th>
                            <th>Hinweis</th>
                        </tr>
                    </thead>
                    <tbody>
                        @forelse ($board['draft_surveys'] ?? [] as $survey)
                            <tr>
                                <td>
                                    <strong>{{ $survey->title() }}</strong>
                                    <div class="muted">ID {{ $survey->id() }}</div>
                                </td>
                                <td>{{ $survey->questionCount() }}</td>
                                <td>
                                    @if ($survey->isDraft())
                                        <span class="status">Entwurf</span>
                                    @elseif ($survey->status() === 'in_review')
                                        <span class="status status--warning">Prüfung</span>
                                    @else
                                        <span class="status status--success">Freigegeben</span>
                                    @endif
                                </td>
                                <td class="muted">
                                    {{ $survey->isDraft() ? 'Kann live bearbeitet werden.' : 'Bereit für Snapshot und Freigabe.' }}
                                </td>
                            </tr>
                        @empty
                            <tr>
                                <td colspan="4" class="muted">Keine Umfragen vorhanden.</td>
                            </tr>
                        @endforelse
                    </tbody>
                </table>
            </div>
        </article>

        <article class="panel">
            <h3>Freigabe-Workflow</h3>
            <div class="timeline">
                @foreach ($board['workflow'] ?? [] as $step)
                    <div class="timeline__item">
                        <p class="timeline__title">{{ $step['step'] ?? 'Schritt' }}</p>
                        <p class="timeline__meta">{{ $step['detail'] ?? '' }}</p>
                    </div>
                @endforeach
            </div>
            <div class="note" style="margin-top: 18px;">
                Freigaben können tenantweit deaktivierbar bleiben, der veröffentlichte Stand bleibt trotzdem ein eigener Snapshot.
            </div>
        </article>
    </section>

    <section class="grid grid--2" style="margin-top: 18px;">
        <article class="table-card">
            <div class="table-card__header">
                <div>
                    <p class="card__eyebrow">Veröffentlicht</p>
                    <h3>Snapshots für Mitglieder</h3>
                </div>
                <span class="pill">Read only</span>
            </div>
            <div class="table-card__body">
                <table>
                    <thead>
                        <tr>
                            <th>Snapshot</th>
                            <th>Stand</th>
                            <th>Antworten</th>
                            <th>Freigabe</th>
                        </tr>
                    </thead>
                    <tbody>
                        @forelse ($board['published_snapshots'] ?? [] as $snapshot)
                            <tr>
                                <td>
                                    <strong>{{ $snapshot->surveyTitle() }}</strong>
                                    <div class="muted">{{ $snapshot->versionLabel() }}</div>
                                </td>
                                <td>
                                    <div>{{ $snapshot->publishedAt() }}</div>
                                    <div class="muted">von {{ $snapshot->publishedBy() }}</div>
                                </td>
                                <td>{{ $snapshot->responseCount() }}</td>
                                <td>
                                    @if ($snapshot->memberVisible())
                                        <span class="status status--success">Sichtbar</span>
                                    @else
                                        <span class="status status--warning">Gesperrt</span>
                                    @endif
                                </td>
                            </tr>
                        @empty
                            <tr>
                                <td colspan="4" class="muted">Noch kein Snapshot veröffentlicht.</td>
                            </tr>
                        @endforelse
                    </tbody>
                </table>
            </div>
        </article>

        <article class="panel">
            <h3>Mitglieder-Sicht</h3>
            <div class="stack">
                @foreach ($memberBoard['participation'] ?? [] as $item)
                    <div class="feature-list__item">
                        <div class="feature-list__badge">S</div>
                        <div>
                            <p class="feature-list__title">{{ $item }}</p>
                        </div>
                    </div>
                @endforeach
            </div>
            <div class="note" style="margin-top: 18px;">
                Mitglieder sehen nur freigegebene Snapshots. Entwürfe bleiben für die Fachseite bearbeitbar.
            </div>
        </article>
    </section>

    <section class="grid grid--2" style="margin-top: 18px;">
        <article class="panel">
            <h3>Rollen im Modul</h3>
            <div class="stack">
                @foreach ($board['roles'] ?? [] as $role)
                    <div class="feature-list__item">
                        <div class="feature-list__badge">{{ strtoupper(substr((string) ($role['role'] ?? ''), 0, 1)) }}</div>
                        <div>
                            <p class="feature-list__title">{{ $role['role'] ?? '' }}</p>
                            <p class="feature-list__copy muted">{{ $role['capability'] ?? '' }}</p>
                        </div>
                    </div>
                @endforeach
            </div>
        </article>

        <article class="panel">
            <h3>Regeln für das Publishing</h3>
            <ul class="list-reset">
                @foreach ($board['publishing_rules'] ?? [] as $rule)
                    <li style="margin-bottom: 12px;">
                        <span class="status">Regel</span> {{ $rule }}
                    </li>
                @endforeach
            </ul>
        </article>
    </section>
@endsection
