<?php

namespace App\Modules\Leasing;

use App\Modules\Leasing\Jobs\ImportResidualValuesJob;
use Illuminate\Support\Collection;
use Mtc\MercuryDataModels\KeyloopLeaseResidualValue;

class KeyloopFleetKompactResidualValueImporter extends KeyloopFleetKompactImporterBase
{
    protected array $filtered_variants = [];

    protected function filter(Collection $data): bool
    {
        if (
            array_key_exists((string) $data['fran'], $this->enabled_keyloop_makes)
            && array_key_exists($data['fran'], $this->enabled_keyloop_models)
            && array_key_exists((string) $data['model'], $this->enabled_keyloop_models[$data['fran']])
        ) {
            return true;
        }

        return false;
    }

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

    /**
     * We expect each incoming row to contain all residual values.
     * Columns are expected to be named with a code representing the contract length
     * and annual mileage for the residual value.
     *
     * @param Collection $data
     * @return void
     */
    protected function importItems(Collection $data): void
    {
        $this->dispatch(new ImportResidualValuesJob($data->map(fn ($item) => $item->toArray())));
    }
}
