<?php

namespace Modules\Payment\Exceptions;

use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Modules\Payment\Data\RefundResultData;
use RuntimeException;

/**
 * Thrown by RefundBookingAction after a failed refund attempt is already
 * persisted (refunds.status = failed) — the booking is deliberately left
 * untouched, and the gateway's own message is surfaced to the caller
 * (domain.md §6).
 */
class RefundFailedException extends RuntimeException
{
    public static function fromResult(RefundResultData $result): self
    {
        return new self($result->message ?? 'Refund failed.');
    }

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

        return null;
    }
}
