<?php

namespace Modules\Booking\Exceptions;

use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Modules\Routing\Models\EvRoute;
use RuntimeException;

class InvalidReturnRouteException extends RuntimeException
{
    public static function notReverseOfOutbound(EvRoute $returnRoute, EvRoute $outboundRoute): self
    {
        return new self(
            "Return route [{$returnRoute->id}] is not the reverse of outbound route [{$outboundRoute->id}] — ".
            'from/to destinations must be swapped.'
        );
    }

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

        return null;
    }
}
