<?php

namespace App\Jobs\Truck;

use App\Models\Truck;
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\Arr;
use Illuminate\Support\Facades\Log;

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

    public function __construct(
        protected array $truckData
    ) {}

    public function handle(): void
    {
        Log::info('Deleting truck', ['hexon_id' => Arr::get($this->truckData, 'voertuignr_hexon')]);

        $truck = Truck::query()
            ->where(
                'hexon_id',
                Arr::get($this->truckData, 'voertuignr_hexon')
            )
            ->firstOrFail();

        $truck->media()->delete();
        $truck->brochures()->delete();
        $truck->delete();

        Log::info('Truck deleted', ['hexon_id' => Arr::get($this->truckData, 'voertuignr_hexon')]);
    }
}
