<?php

namespace Modules\Booking\Actions;

use Modules\Booking\Data\AssignDriverData;
use Modules\Booking\Enums\BookingStatus;
use Modules\Booking\Exceptions\DriverAssignmentNotAllowedException;
use Modules\Booking\Models\Booking;

/**
 * Driver/vehicle details only make sense once a booking is confirmed
 * (paid) — dispatch assigns who's actually doing the trip at that point,
 * not before. Re-running this (e.g. reassigning a different driver) is
 * allowed as long as the booking is still confirmed.
 */
class AssignDriverAction
{
    public function handle(Booking $booking, AssignDriverData $data): Booking
    {
        if ($booking->status !== BookingStatus::Confirmed) {
            throw DriverAssignmentNotAllowedException::notConfirmed($booking);
        }

        $booking->update([
            'driver_name' => $data->driverName,
            'driver_phone' => $data->driverPhone,
            'car_plate_number' => $data->carPlateNumber,
            'car_model' => $data->carModel,
        ]);

        return $booking;
    }
}
