@extends('layouts.app')

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

@php
    $overview = $paymentOverview ?? [];
    $journal = $payments ?? ($overview['payments'] ?? []);
    $summary = $overview['summary'] ?? [];
    $paymentCount = (int) ($summary['count'] ?? count($journal));
    $paymentTotal = (float) ($summary['total_amount'] ?? array_sum(array_map(static fn (array $row): float => (float) ($row['amount'] ?? 0), $journal)));
    $latestPayment = $summary['latest_booking'] ?? ($journal[0]['booked_at'] ?? null);
    $cancelableCount = (int) ($summary['cancelable_payments'] ?? count(array_filter($journal, static fn (array $row): bool => (bool) ($row['cancellable'] ?? false))));
    $reconciliationPendingCount = (int) ($summary['reconciliation_pending'] ?? count(array_filter($journal, static fn (array $row): bool => ($row['reconciliation_state'] ?? 'neu') !== 'gematcht')));
    $reconciliation = $overview['reconciliation'] ?? [];
    $paymentMethods = [
        ['label' => 'Bar', 'state' => 'direkt'],
        ['label' => 'Ueberweisung', 'state' => 'tenant-konfigurierbar'],
        ['label' => 'PayPal', 'state' => 'spaeteres Folgepaket'],
    ];
    $reconciliationStates = ['neu', 'gematcht', 'pruefen', 'storniert'];
@endphp

@section('content')
    <section class="hero hero--split">
        <div class="hero__content">
            <div>
                <p class="hero__kicker">Zahlungen</p>
                <h2 class="hero__title">Zahlungen</h2>
                <p class="hero__lead">
                    Das Payments-Modul bildet die fachlich gewollte Konfiguration fuer Bar, Ueberweisung und PayPal ab.
                    Reconciliation bleibt bewusst als Folgepaket vorbereitet, damit der MVP bei manuellen Buchungen stabil bleibt.
                </p>
            </div>
            <div class="hero__actions">
                <span class="badge badge--solid">Finance workflow</span>
                <span class="badge">Tenant scoped</span>
                <span class="badge">Reconciliation vorbereitet</span>
                <span class="badge">Storno nur durch Verantwortliche</span>
            </div>
            <div class="hero__meta">
                <span class="pill">{{ $paymentCount }} Einzahlungen</span>
                <span class="pill">{{ number_format($paymentTotal, 2, ',', '.') }} EUR</span>
                <span class="pill">Letzte Buchung: {{ $latestPayment ?? '-' }}</span>
                <span class="pill">{{ $cancelableCount }} stornierbar</span>
            </div>
        </div>

        <aside class="hero__aside">
            <article class="card metric">
                <p class="metric__label">Zahlungsarten</p>
                <div class="metric__value">3 Pfade</div>
                <p class="muted">Bar, Ueberweisung und PayPal werden tenantbezogen konfigurierbar.</p>
            </article>
            <article class="card metric">
                <p class="metric__label">Abgleich</p>
                <div class="metric__value">Folgepaket</div>
                <p class="muted">Statusfelder fuer spaetere Matching- und Import-Prozesse sind vorgesehen.</p>
            </article>
        </aside>
    </section>

    <section class="grid grid--3">
        <article class="card metric">
            <p class="metric__label">Gebuchte Einzahlungen</p>
            <div class="metric__value">{{ $paymentCount }}</div>
            <p class="muted">Manuelle Zahlungen und importierte Eintraege werden hier zusammengefuehrt.</p>
        </article>
        <article class="card metric">
            <p class="metric__label">Saldo-Effekt</p>
            <div class="metric__value">{{ number_format($paymentTotal, 2, ',', '.') }} EUR</div>
            <p class="muted">Der Betrag wirkt unmittelbar auf den Mitgliedssaldo im Ledger.</p>
        </article>
            <article class="card metric">
                <p class="metric__label">Reconciliation-Status</p>
                <div class="metric__value">{{ $reconciliationPendingCount }}</div>
                <p class="muted">Eintraege sind bereits mit vorbereiteten Statusfeldern versehen.</p>
            </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">Zahlungsjournal</p>
                    <h3>Letzte Zahlungseintraege</h3>
                </div>
                <span class="pill">Finance Sicht</span>
            </div>
            <div class="table-card__body">
                <table>
                    <thead>
                        <tr>
                            <th>Mitglied</th>
                            <th>Methode</th>
                            <th>Betrag</th>
                            <th>Status</th>
                            <th>Buchung</th>
                        </tr>
                    </thead>
                    <tbody>
                        @forelse ($journal as $entry)
                            <tr>
                                <td>{{ $entry['member_name'] ?? '-' }}</td>
                                <td>{{ ucfirst((string) ($entry['payment_method'] ?? 'manual')) }}</td>
                                <td>{{ number_format((float) ($entry['amount'] ?? 0), 2, ',', '.') }} EUR</td>
                                <td>
                                    @if (($entry['reconciliation_state'] ?? 'neu') === 'gematcht')
                                        <span class="status">Gematcht</span>
                                    @elseif (($entry['reconciliation_state'] ?? 'neu') === 'pruefen')
                                        <span class="status status--warning">Pruefen</span>
                                    @else
                                        <span class="status">Stornierbar</span>
                                    @endif
                                </td>
                                <td>
                                    @if (!empty($entry['cancellable']))
                                        <span class="status status--warning">Nur Verantwortliche</span>
                                    @else
                                        <span class="status">Archiviert</span>
                                    @endif
                                </td>
                            </tr>
                        @empty
                            <tr>
                                <td colspan="5"><span class="muted">Noch keine Zahlungen vorhanden.</span></td>
                            </tr>
                        @endforelse
                    </tbody>
                </table>
            </div>
        </article>

        <article class="panel">
            <p class="card__eyebrow">Reconciliation</p>
            <h3>Vorbereitung fuer spaetere Abgleiche</h3>
            <div class="timeline" style="margin-top: 18px;">
                <div class="timeline__item">
                    <p class="timeline__title">Statusmodell</p>
                    <p class="timeline__meta">{{ implode(', ', $reconciliation['states'] ?? $reconciliationStates) }}</p>
                </div>
                <div class="timeline__item">
                    <p class="timeline__title">Storno-Regel</p>
                    <p class="timeline__meta">{{ $reconciliation['responsible_rule'] ?? 'Einzahlungen koennen nur durch Verantwortliche storniert werden.' }}</p>
                </div>
                <div class="timeline__item">
                    <p class="timeline__title">MVP</p>
                    <p class="timeline__meta">{{ $reconciliation['message'] ?? 'Nur Konfiguration und manuelle Buchung, kein automatischer Mailbox-Import.' }}</p>
                </div>
                <div class="timeline__item">
                    <p class="timeline__title">Spaeter</p>
                    <p class="timeline__meta">Matching, externe Referenz und Klaerungsfaelle werden als Folgepaket ergaenzt.</p>
                </div>
            </div>
            <div class="note" style="margin-top: 18px;">
                Das Payments-Modul trennt die fachliche Konfiguration von spaeterer Automatisierung, damit der MVP stabil bleibt.
            </div>
        </article>
    </section>

    <section class="grid grid--3" style="margin-top: 18px;">
        @foreach ($paymentMethods as $method)
            <article class="card">
                <p class="card__eyebrow">{{ $method['label'] }}</p>
                <h3>{{ $method['state'] }}</h3>
                <p class="muted">
                    {{ $method['label'] }} bleibt tenantbezogen konfigurierbar und kann mit sichtbaren Pflichtfeldern versehen werden.
                </p>
            </article>
        @endforeach
    </section>
@endsection
