<?php

declare(strict_types=1);

namespace App\Modules\Payments\Application;

use App\Modules\Payments\Domain\Payment;

class PaymentService
{
    /**
     * @return array<int, Payment>
     */
    public function recentPayments(string $tenantId, string $memberId): array
    {
        return [
            new Payment(
                id: 'payment-demo-1',
                tenantId: $tenantId,
                memberId: $memberId,
                amount: 10.00,
                method: 'manual',
                bookedAt: '2026-03-20 09:00:00',
                reconciliationState: 'gematcht'
            ),
            new Payment(
                id: 'payment-demo-2',
                tenantId: $tenantId,
                memberId: $memberId,
                amount: 5.00,
                method: 'cash',
                bookedAt: '2026-03-22 13:15:00',
                reconciliationState: 'pruefen'
            ),
        ];
    }

    /**
     * @return array<string, mixed>
     */
    public function paymentOverview(string $tenantId, string $memberId): array
    {
        $payments = array_map(
            static fn (Payment $payment): array => $payment->toArray(),
            $this->recentPayments($tenantId, $memberId)
        );

        return [
            'payments' => $payments,
            'methods' => [
                [
                    'key' => 'cash',
                    'label' => 'Bar',
                    'state' => 'direkt',
                ],
                [
                    'key' => 'bank',
                    'label' => 'Ueberweisung',
                    'state' => 'vorbereitet',
                ],
                [
                    'key' => 'paypal',
                    'label' => 'PayPal',
                    'state' => 'tenant-konfigurierbar',
                ],
            ],
            'reconciliation' => [
                'status' => 'planned',
                'message' => 'Reconciliation wird als Folgepaket vorbereitet, nicht als MVP-Import.',
                'states' => ['neu', 'gematcht', 'pruefen', 'storniert'],
                'responsible_rule' => 'Storno und Korrekturen werden nur durch Verantwortliche ausgelöst.',
            ],
            'summary' => [
                'count' => (string) count($payments),
                'total_amount' => array_sum(array_map(static fn (array $payment): float => (float) ($payment['amount'] ?? 0), $payments)),
                'latest_booking' => $payments[0]['booked_at'] ?? null,
                'cancelable_payments' => count(array_filter($payments, static fn (array $payment): bool => (bool) ($payment['cancellable'] ?? false))),
                'reconciliation_pending' => count(array_filter($payments, static fn (array $payment): bool => ($payment['reconciliation_state'] ?? 'neu') !== 'gematcht')),
            ],
        ];
    }
}
