<?php

namespace Modules\Booking\Exceptions;

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

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

    public static function travelDateInPast(Booking $booking): self
    {
        return new self(
            "Booking [{$booking->booking_ref}] cannot have a driver assigned because its travel date [{$booking->travel_date->toDateString()}] is in the past."
        );
    }

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

        return null;
    }
}
