<?php

namespace App\Modules\Stock;

use App\Contracts\StockProvider;
use App\Facades\Settings;
use App\Imports\DealerInternetImport;
use App\Traits\TracksImportStats;

class DealerInternet implements StockProvider
{
    use TracksImportStats;

    public function enabled(): bool
    {
        return Settings::get('stock-dealer-internet-enabled', false) ?? false;
    }

    public function name(): string
    {
        return 'dealer-internet-import';
    }

    public function runScheduledImport(bool $fullSync = true): void
    {
        $this->resetStats();
        $import = new DealerInternetImport();
        $import->importVehicles();
        $this->aggregateStats($import->getImportStats());
    }

    private function aggregateStats(array $stats): void
    {
        for ($i = 0; $i < $stats['created']; $i++) {
            $this->incrementCreated();
        }
        for ($i = 0; $i < $stats['updated']; $i++) {
            $this->incrementUpdated();
        }
        for ($i = 0; $i < $stats['failed']; $i++) {
            $this->incrementFailed();
        }
    }
}
