<?php

namespace Modules\Payment\Listeners;

use Illuminate\Contracts\Queue\ShouldQueue;
use Modules\Booking\Enums\BookingStatus;
use Modules\Payment\Events\PaymentCompleted;

/**
 * Flips the Booking to confirmed once its Payment is confirmed successful
 * (domain.md §5). Queued — ConfirmPaymentAction's own idempotency check is
 * what actually prevents this firing twice for the same Payment; the guard
 * here is just to never clobber a booking that moved on for another reason.
 */
class MarkBookingPaid implements ShouldQueue
{
    public function handle(PaymentCompleted $event): void
    {
        $booking = $event->payment->booking;

        if ($booking->status === BookingStatus::PendingPayment) {
            $booking->update(['status' => BookingStatus::Confirmed]);
        }
    }
}
