<?php

namespace App\Modules\Leasing;

use App\Modules\Leasing\Jobs\ImportOptionsJob;
use Illuminate\Support\Collection;
use Mtc\MercuryDataModels\KeyloopLeaseOption;

class KeyloopFleetKompactOptionImporter extends KeyloopFleetKompactImporterBase
{
    protected function filter(Collection $data): bool
    {
        if (
            array_key_exists($data['franchise-code'], $this->enabled_keyloop_makes)
            && array_key_exists($data['franchise-code'], $this->enabled_keyloop_models)
            && array_key_exists($data['model-code'], $this->enabled_keyloop_models[$data['franchise-code']])
        ) {
            return true;
        }

        return false;
    }

    protected function prepareForImport(): void
    {
        KeyloopLeaseOption::query()->update([
            'was_recently_synced' => false,
        ]);
    }

    protected function importItems(Collection $data): void
    {
        $this->dispatch(new ImportOptionsJob($data->map(fn ($item) => [
            'external_variant_id' => $item['variant-code'],
            'make_id' => $this->getVehicleMake($item['franchise-code']),
            'model_id' => $this->getVehicleModel($item['model-code'], $item['franchise-code']),
            'option_type' => $item['option-type'],
            'option_code' => $item['option-code'],
            'name' => $this->getUtf8String($item['description']),
            'default_vat_code' => $item['default-vat-code'],
        ])));
    }
}
