<?php

namespace App\Listeners;

use App\Events\PaymentProofReviewed;
use App\Notifications\PaymentConfirmedNotification;
use App\Notifications\PaymentProofSubmittedNotification;
use App\Notifications\PaymentRejectedNotification;

class SendPaymentProofNotification
{
    public bool $afterCommit = true;

    public function handle(PaymentProofReviewed $event): void
    {
        $proof = $event->proof->loadMissing(['eventRegistration.event', 'eventRegistration.user']);
        $user = $proof->eventRegistration->user;

        match ($event->action) {
            PaymentProofReviewed::ACTION_SUBMITTED => $user->notify(new PaymentProofSubmittedNotification($proof)),
            PaymentProofReviewed::ACTION_CONFIRMED => $user->notify(new PaymentConfirmedNotification($proof)),
            PaymentProofReviewed::ACTION_REJECTED => $user->notify(new PaymentRejectedNotification($proof, $event->reason)),
            default => null,
        };
    }
}
