<?php

namespace App\Command;

use App\Helper\HiltesImport;
use App\Helper\Jtl;
use App\Repository\ProductRepository;
use App\Repository\StockRepository;
use App\Repository\WarehouseRepository;
use Psr\Log\LoggerInterface;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;

use Symfony\Component\Finder\Finder;

#[AsCommand(
    name: 'hiltes:import',
    description: 'Importiert die Bestandsdaten von Hiltes',
)]
class HiltesImportCommand extends Command
{
    private $stockRepository;
    private $warehouseRepository;
    private $productRepository;
    private $logger;
    public function __construct(
        ProductRepository $productRepository,
        StockRepository $stockRepository,
        WarehouseRepository $warehouseRepository,
        LoggerInterface $logger
    )
    {
        $this->productRepository = $productRepository;
        $this->stockRepository = $stockRepository;
        $this->warehouseRepository = $warehouseRepository;
        $this->logger = $logger;
        parent::__construct();
    }
    protected function configure(): void
    {
        $this->addOption('delta', 'd', InputOption::VALUE_NONE, 'Delta Import');
    }

    protected function execute(InputInterface $input, OutputInterface $output): int
    {
        $io = new SymfonyStyle($input, $output);

        $io->success('Start Hiltes Import');

		/**
		 * @var HiltesImport
		 */
        $hiltesImport = new HiltesImport($this->productRepository,$this->warehouseRepository,$this->stockRepository,$this->logger);


        $r = $hiltesImport->startImport($input->getOption('delta'));

        if(isset($r['error'])){
            $io->error($r['text']);
            $this->logger->error($r['text']);
            return Command::FAILURE;
        }else{
            $io->info('Start Hiltes Push JTL');

            $jtl = new Jtl($this->productRepository,$this->warehouseRepository,$this->stockRepository,$this->logger);
            $jtl->createExportFile($jtl->getProducts());

            $io->success('Done.');
            return Command::SUCCESS;
        }
    }
}