<?php

namespace Modules\Payment\Exceptions;

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

class RefundNotAllowedException extends RuntimeException
{
    public static function notConfirmed(Booking $booking): self
    {
        return new self(
            "Booking [{$booking->booking_ref}] cannot be refunded because its status is [{$booking->status->value}], not confirmed."
        );
    }

    public static function noCompletedPayment(Booking $booking): self
    {
        return new self("Booking [{$booking->booking_ref}] has no completed payment to refund.");
    }

    public static function exceedsRefundableBalance(Payment $payment, string $amount, string $remaining): self
    {
        return new self(
            "Refund amount [{$amount}] exceeds the remaining refundable balance [{$remaining}] on payment [{$payment->id}]."
        );
    }

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

        return null;
    }
}
