<?php
declare(strict_types=1);

namespace Autogedal\Manufacturers\Cron;

use Autogedal\Manufacturers\Model\Config;
use Autogedal\Manufacturers\Service\ManufacturerGenerator;
use Psr\Log\LoggerInterface;

class UpdateManufacturers
{
    public function __construct(
        private readonly ManufacturerGenerator $generator,
        private readonly Config $config,
        private readonly LoggerInterface $logger
    ) {
    }

    public function execute(): void
    {
        if (!$this->config->isEnabled()) {
            return;
        }

        try {
            $this->logger->info('Cron Autogedal_Manufacturers: Start checking for new manufacturers.');
            $this->generator->generate();
            $this->logger->info('Cron Autogedal_Manufacturers: Finished checking for new manufacturers.');
        } catch (\Throwable $exception) {
            $this->logger->error(
                'Cron Autogedal_Manufacturers Error: ' . $exception->getMessage(),
                ['exception' => $exception]
            );
        }
    }
}
