@extends('layouts.app')

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

@section('content')
    <section class="hero">
        <div>
            <p class="hero__kicker">Benachrichtigungen</p>
            <h2 class="hero__title">Massenkommunikation pro Tenant</h2>
            <p class="hero__lead">
                Hier entstehen Hinweise, Erinnerungen und Service-Meldungen fuer alle oder
                eine Teilmenge der Mitglieder. Entweder als Entwurf, geplant oder direkt
                als Versandprotokoll.
            </p>
        </div>
        <div class="toolbar">
            <span class="badge">Kampagnen: {{ number_format((int) ($notificationSummary['total'] ?? 0), 0, ',', '.') }}</span>
            <span class="badge">Empfaenger: {{ number_format((int) ($notificationSummary['recipient_count'] ?? 0), 0, ',', '.') }}</span>
            <span class="badge badge--solid">{{ $tenantLicense['plan_name'] ?? 'Free' }}</span>
        </div>
    </section>

    @if(!$notificationStorageReady)
        <section class="panel">
            <h3>Fallback aktiv</h3>
            <p class="muted">
                Die Datenbanktabelle fuer Benachrichtigungen ist noch nicht vorhanden.
                Solange die Migration nicht gelaufen ist, werden Demo-Daten angezeigt.
            </p>
        </section>
    @endif

    @if(!empty($dbError))
        <section class="panel">
            <h3>Datenbankhinweis</h3>
            <p class="muted">{{ $dbError }}</p>
        </section>
    @endif

    <section class="split">
        <article class="panel">
            <h3>Neue Benachrichtigung</h3>
            <p class="muted">Damit lassen sich Erinnerungen und Hinweise direkt als Entwurf oder Sofortversand anlegen.</p>

            <form method="post" class="stack">
                <input type="hidden" name="csrf" value="{{ $csrf }}">
                <input type="hidden" name="notification_id" value="{{ $notificationForm['notification_id'] ?? '' }}">

                <label>Titel
                    <input type="text" name="title" value="{{ $notificationForm['title'] ?? '' }}" placeholder="Saldo-Erinnerung">
                </label>

                <label>Nachricht
                    <textarea name="message" rows="8" placeholder="Freundliche Erinnerung an offene Beträge oder wichtige Hinweise.">{{ $notificationForm['message'] ?? '' }}</textarea>
                </label>

                <div class="grid grid--2">
                    <label>Kanal
                        <select name="channel">
                            @foreach($notificationChannelOptions as $option)
                                <option value="{{ $option['value'] }}" @selected(($notificationForm['channel'] ?? 'email') === $option['value'])>{{ $option['label'] }}</option>
                            @endforeach
                        </select>
                    </label>

                    <label>Empfaenger
                        <select name="recipient_scope">
                            @foreach($notificationScopeOptions as $option)
                                <option value="{{ $option['value'] }}" @selected(($notificationForm['recipient_scope'] ?? 'all') === $option['value'])>{{ $option['label'] }}</option>
                            @endforeach
                        </select>
                    </label>
                </div>

                <label>Geplanter Versand
                    <input type="datetime-local" name="scheduled_at" value="{{ $notificationForm['scheduled_at'] ?? '' }}">
                </label>

                <div class="toolbar">
                    <button class="button button--ghost" type="submit" name="action" value="save-notification">Entwurf speichern</button>
                    <button class="button" type="submit" name="action" value="send-notification">Jetzt senden</button>
                </div>
            </form>
        </article>

        <article class="panel">
            <h3>Empfängervorschau</h3>
            <p class="muted">Die Vorschau folgt dem aktuellen Filter und dient als schnelle Kontrolle vor dem Versand.</p>

            <div class="toolbar">
                @foreach($notificationScopeOptions as $option)
                    <a class="badge {{ ($notificationForm['recipient_scope'] ?? 'all') === $option['value'] ? 'badge--solid' : '' }}" href="/notifications/?scope={{ urlencode($option['value']) }}@if(!empty($notificationForm['notification_id']))&amp;edit={{ urlencode((string) $notificationForm['notification_id']) }}@endif">{{ $option['label'] }}</a>
                @endforeach
            </div>

            <div class="timeline">
                @forelse($notificationPreview as $member)
                    <div class="timeline__item">
                        <p class="timeline__title">{{ $member['display_name'] ?? 'Mitglied' }}</p>
                        <p class="timeline__meta">
                            {{ number_format((int) ($member['recent_strokes'] ?? 0), 0, ',', '.') }} Striche in 100 Tagen
                        </p>
                    </div>
                @empty
                    <div class="timeline__item">
                        <p class="timeline__title">Keine aktiven Mitglieder</p>
                        <p class="timeline__meta">Fuer die gewaehlte Zielgruppe stehen gerade keine Empfaenger zur Verfuegung.</p>
                    </div>
                @endforelse
            </div>
        </article>
    </section>

    <section class="split">
        <article class="table-card">
            <div class="table-card__header">
                <div>
                    <p class="card__eyebrow">Kampagnen</p>
                    <h3>Geplante und gesendete Nachrichten</h3>
                </div>
                <span class="pill">Tenant scoped</span>
            </div>
            <div class="table-card__body">
                <table>
                    <thead>
                        <tr>
                            <th>Titel</th>
                            <th>Kanal</th>
                            <th>Empfaenger</th>
                            <th>Status</th>
                            <th>Aktion</th>
                        </tr>
                    </thead>
                    <tbody>
                        @forelse($notificationMessages as $message)
                            <tr>
                                <td>
                                    <strong>{{ $message['title'] ?? '' }}</strong><br>
                                    <span class="muted">{{ $message['message'] ?? '' }}</span>
                                </td>
                                <td>{{ $message['channel_label'] ?? 'E-Mail' }}</td>
                                <td>
                                    {{ $message['scope_label'] ?? 'Alle Mitglieder' }}<br>
                                    <span class="muted">{{ number_format((int) ($message['recipient_count'] ?? 0), 0, ',', '.') }} Personen</span>
                                </td>
                                <td><span class="status status--{{ $message['status_tone'] ?? 'neutral' }}">{{ $message['status_label'] ?? 'Entwurf' }}</span></td>
                                <td>
                                    <a class="status status--warning" href="/notifications/?edit={{ urlencode((string) ($message['id'] ?? '')) }}">Bearbeiten</a>
                                </td>
                            </tr>
                        @empty
                            <tr>
                                <td colspan="5">Noch keine Benachrichtigungen gespeichert.</td>
                            </tr>
                        @endforelse
                    </tbody>
                </table>
            </div>
        </article>

        <article class="table-card">
            <div class="table-card__header">
                <div>
                    <p class="card__eyebrow">Versandprotokoll</p>
                    <h3>Letzte Logeintraege</h3>
                </div>
                <span class="pill">Audit trail</span>
            </div>
            <div class="table-card__body">
                <table>
                    <thead>
                        <tr>
                            <th>Kanal</th>
                            <th>Empfaenger</th>
                            <th>Typ</th>
                            <th>Status</th>
                        </tr>
                    </thead>
                    <tbody>
                        @forelse($notificationLogs as $log)
                            <tr>
                                <td>{{ $log['channel_label'] ?? 'E-Mail' }}</td>
                                <td>{{ $log['recipient'] ?? '-' }}</td>
                                <td>{{ $log['template_key'] ?? '-' }}</td>
                                <td><span class="status status--{{ $log['status_tone'] ?? 'neutral' }}">{{ $log['status_label'] ?? 'Geplant' }}</span></td>
                            </tr>
                        @empty
                            <tr>
                                <td colspan="4">Noch keine Versandprotokolle vorhanden.</td>
                            </tr>
                        @endforelse
                    </tbody>
                </table>
            </div>
        </article>
    </section>

    <section class="panel">
        <h3>Einordnung</h3>
        <p class="muted">
            Das Modul ist bewusst schlank gehalten: Kampagne anlegen, Empfaenger pruefen, speichern oder sofort versenden.
            Die eigentliche Auslieferung kann spaeter ueber den Cronjob mit der vorhandenen Dispositionslogik erweitert werden.
        </p>
    </section>
@endsection
