<?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;

#[AsCommand(
    name: 'jtl:export',
    description: 'Create a export file for JTL Ameise',
)]
class JtlExportCommand 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 JTL Export');
        $rootPath = $this->GetProjectRootDir();
        $delta = $input->getOption('delta');

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


        //Export für Standartlager
        $jtl->createExportFile($jtl->getProducts([0], ['1', '3', '5', '10']), 'standard' . ($delta ? '_delta' : ''));

        //Export für WMS Lager
        $jtl->createExportFile($jtl->getProducts([0], ['8']), 'wms' . ($delta ? '_delta' : ''));


        $io->success('Ende  JTL Export');

        return Command::SUCCESS;
    }

    public static function GetProjectRootDir()
    {
        $dirFullPath = __DIR__;

        //PRE: $dirs = /app/public/src/Helpers
        $dirs = explode('/', $dirFullPath);

        array_pop($dirs); //remove last element in array ('Helpers')
        array_pop($dirs); //remove the next last element from array ('src')

        //POST: $dirs = /app/public
        return implode('/', $dirs);
    }

}