<?php

namespace Modules\Booking\Exceptions;

use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Modules\Booking\Models\Booking;
use RuntimeException;

class BookingCannotBeCancelledException extends RuntimeException
{
    /**
     * Confirmed bookings no longer reach this — CancelBookingAction (T5.12)
     * refunds them instead. This is only for statuses that can't be
     * cancelled at all (already cancelled/expired).
     */
    public static function notPendingPayment(Booking $booking): self
    {
        return new self(
            "Booking [{$booking->booking_ref}] cannot be cancelled because its status is [{$booking->status->value}]."
        );
    }

    /**
     * A rejected cancel attempt is a client input problem, not a server
     * error — surface it as 422 rather than the default 500.
     */
    public function render(Request $request): ?JsonResponse
    {
        if ($request->expectsJson()) {
            return response()->json(['message' => $this->getMessage()], 422);
        }

        return null;
    }
}
