<?php

declare(strict_types=1);

/*
 * This file is part of vonRotenberg WMFGO Cevisio Bundle.
 *
 * (c) vonRotenberg
 *
 * @license proprietary
 */

namespace vonRotenberg\AldegottSwNlBundle\Command;

use Contao\CoreBundle\Framework\ContaoFramework;
use Contao\System;
use Contao\Validator;
use Symfony\Component\Console\Command\Command;
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 vonRotenberg\AldegottSwNlBundle\Cron\ShopwareExportNewsletterJob;
use vonRotenberg\AldegottSwNlBundle\Cron\ShopwareExportOrdersJob;
use vonRotenberg\AldegottSwNlBundle\Cron\ShopwareImportProductsJob;

class ShopwareExportNewsletterCommand extends Command
{
    protected static $defaultName = 'shopware:export-newsletter-recipients';

    /**
     * @var ContaoFramework
     */
    private $framework;

    private $start;
    private $stop;
    private $email;

    public function __construct(ContaoFramework $framework)
    {
        $this->framework = $framework;

        parent::__construct();
    }

    protected function configure(): void
    {
        $this
            ->setName(self::$defaultName)
            ->addOption('start', 'b', InputOption::VALUE_REQUIRED, 'Custom start date')
            ->addOption('stop', 'x', InputOption::VALUE_REQUIRED, 'Custom stop date')
            ->addOption('email', 't', InputOption::VALUE_OPTIONAL, 'Email address to send exported recipients file to')
            ->setDescription('Export newsletter recipients from Shopware instance')
        ;
    }

    protected function interact(InputInterface $input, OutputInterface $output): void
    {
        if ((null !== $input->getOption('start') && null === $input->getOption('stop'))
            || (null === $input->getOption('start') && null !== $input->getOption('stop')))
        {
            throw new \RuntimeException('You need to provide start and stop date.');
        }

        if (null !== $input->getOption('start')) {
            try {
                new \DateTime($input->getOption('start'));
                $this->start = $input->getOption('start');
            } catch(\Exception $e)
            {
                throw new \InvalidArgumentException('Start date is invalid. Please provide an ISO date.');
            }
        }

        if (null !== $input->getOption('stop')) {
            try {
                new \DateTime($input->getOption('stop'));
                $this->stop = $input->getOption('stop');
            } catch(\Exception $e)
            {
                throw new \InvalidArgumentException('Stop date is invalid. Please provide an ISO date.');
            }
        }

        if (null !== $input->getOption('email')) {
            if (Validator::isEmail($input->getOption('email')))
            {
                $this->email = $input->getOption('email');
            } else
            {
                throw new \InvalidArgumentException('Stop date is invalid. Please provide an ISO date.');
            }
        }
    }

    protected function execute(InputInterface $input, OutputInterface $output): int
    {
        $this->framework->initialize();

        $io = new SymfonyStyle($input, $output);
        $io->title('Newsletter recipients export from Shopware');

        /** @var ShopwareExportNewsletterJob $export */
        $export = System::getContainer()->get(ShopwareExportNewsletterJob::class);

        $export->export('cli', $this->start,$this->stop, $this->email,$io);

        return 0;
    }
}