<?php
namespace App\Command;

use App\ExactApi\IsotopeDatabaseHandler;
use App\ExactApi\ApiExact;
use Doctrine\DBAL\Connection;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class CmdUpdateProducts extends Command
{
    protected static $defaultName = 'app:update-products';
    protected $connection;
    private $statusCode = 0;

    public function __construct(Connection $connection)
    {
        $this->connection = $connection;
        parent::__construct();
    }

    protected function configure(): void
    {
        $this->setDescription('Updates products from Exact-Online to Contao-Isotope tables.');
    }

    protected function execute(InputInterface $input, OutputInterface $output): ?int
    {
        $apiConnector = new ApiExact();
        $products = $apiConnector->getProducts();
        $handler = new IsotopeDatabaseHandler($this->connection);
        $handler->processProducts($products);
        return $this->statusCode;
    }
}