<?php
declare(strict_types=1);

use App\Core\EventBus;
use App\Core\Logger;
use App\Modules\Waiver\Services\WaiverProcessor;

// Auto-complete waiver after fee payment
EventBus::listen('waiver.fee_paid', function (array $data): void {
    $waiverId = (int) ($data['waiver_id'] ?? 0);
    if ($waiverId <= 0) return;

    try {
        $result = WaiverProcessor::autoComplete($waiverId);
        if (!$result['success']) {
            Logger::info("Waiver #{$waiverId} auto-complete deferred: " . ($result['reason'] ?? 'unknown'), $data);
        }
    } catch (\Throwable $e) {
        Logger::error("Waiver auto-complete failed for #{$waiverId}: " . $e->getMessage(), $data);
    }
});
