<?php

namespace Modules\Payment\Exceptions;

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

class PaymentInitiationNotAllowedException extends RuntimeException
{
    public static function notPendingPayment(Booking $booking): self
    {
        return new self(
            "Booking [{$booking->booking_ref}] cannot have a payment initiated because its status is [{$booking->status->value}], not pending_payment."
        );
    }

    /**
     * A round trip's payment is combined on the outbound leg — the return
     * leg is marked paid when the outbound leg's payment succeeds
     * (MarkBookingPaid), never via its own Payment (domain.md §2b).
     */
    public static function isReturnLeg(Booking $booking): self
    {
        return new self(
            "Booking [{$booking->booking_ref}] is a round trip's return leg — initiate payment on its linked outbound booking instead."
        );
    }

    public function render(Request $request): ?JsonResponse
    {
        if ($request->expectsJson()) {
            return response()->json(['message' => $this->getMessage()], 422);
        }

        return null;
    }
}
