<?php

namespace App\VehicleSpec\Jobs;

use App\VehicleSpec\Services\CapSectorBodyTypeService;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Log;
use Mtc\MercuryDataModels\Vehicle;

class ApplyCapSectorBodyType implements ShouldQueue
{
    use Dispatchable;
    use InteractsWithQueue;
    use Queueable;
    use SerializesModels;

    public function __construct(
        protected readonly Vehicle $vehicle
    ) {
        $this->onQueue('specs');
    }

    public function handle(): void
    {
        App::make(CapSectorBodyTypeService::class)->applyForVehicle($this->vehicle);
    }

    public function failed(?\Throwable $exception): void
    {
        Log::error($exception?->getMessage() ?? 'ApplyCapSectorBodyType failed', [
            'tenant' => tenant('id'),
            'vehicle_id' => $this->vehicle->id,
            'exception' => $exception,
        ]);
    }
}
