<?php

namespace App\Command;

use App\Controller\ShopwareController;
use App\Entity\Order;
use App\Helper\Shopware;
use App\Repository\OrdersRepository;

use Doctrine\ORM\Mapping as ORM;
use Psr\Log\LoggerInterface;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;

use Vin\ShopwareSdk\Client\AdminAuthenticator;
use Vin\ShopwareSdk\Client\GrantType\ClientCredentialsGrantType;
use Vin\ShopwareSdk\Data\Context;
use Vin\ShopwareSdk\Data\Criteria;
use Vin\ShopwareSdk\Data\Entity\EntityCollection;
use Vin\ShopwareSdk\Data\Entity\Order\OrderDefinition;
use Vin\ShopwareSdk\Data\Filter\EqualsAnyFilter;

use Vin\ShopwareSdk\Data\Filter\EqualsFilter;
use Vin\ShopwareSdk\Factory\RepositoryFactory;

#[AsCommand(
    name: 'sw:get-orders',
    description: 'Holt alle offenen Bestellungen von Shopware ab',
)]
class SwGetOrdersCommand extends Command
{
    private $orderRepository;
    private $logger;
    private $sw;
    private $orderData;


    public function __construct(OrdersRepository $orderRepository, LoggerInterface $logger)
    {
        $this->orderRepository = $orderRepository;
        $this->logger = $logger;
        $this->sw = new Shopware($logger);

        parent::__construct();
    }

    protected function configure(): void
    {
//        $this
//            ->addArgument('arg1', InputArgument::OPTIONAL, 'Argument description')
//            ->addOption('option1', null, InputOption::VALUE_NONE, 'Option description')
//        ;
    }

    protected function execute(InputInterface $input, OutputInterface $output): int
    {
        $io = new SymfonyStyle($input, $output);
//        $arg1 = $input->getArgument('arg1');
//
//        if ($arg1) {
//            $io->note(sprintf('You passed an argument: %s', $arg1));
//        }
//
//        if ($input->getOption('option1')) {
//            // ...
//        }

        //offene Bestellungen aus Datenbank holen
        $this->orderData = $this->getOrders();

        $this->getOrderDetails();

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

    /**
     * @return array
     */
    private function getOrders():array
    {
        return $this->orderRepository->findAll();
    }

    /**
     * @return void
     */
    public function getOrderDetails(): void
    {
      //Bestelldetails aus SW holen

        $this->getOrdersDataFromSW();

        foreach ($this->orderData as $order) {
            $this->saveOrdersData($order);
        }
    }


  /**
    * holt alle fehlende Bestelldetails aus SW
   */
    private function getOrdersDataFromSW(): void
    {

        foreach ($this->orderData as $value) {
          // Bei Shopware API anmelden
           $value->setData((array)$this->sw->getOrders($value));
        }
    }

    /**
    * @param $orderData
    * @return void
    */
    private function saveOrdersData($orderData): void
    {
        $orderData->setStatus = 1;
        $this->ordersRepository->add($orderData, true);
    }
}