<?php 

namespace NPS\Command;

use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Validator\Constraints\DateTime;
use Doctrine\ORM\EntityManagerInterface;
use NPS\DependencyInjection\IKnowledgeBaseInfoProvider;
use NPS\Etc\Strings;
use NPS\Model\KnowledgeBase;
use NPS\Repository\KnowledgeBaseRepository;
use NPS\Factory\KnowledgeBaseFactory;



/**
 * Disables a knowledge base
 * @author Lukas Korinek
 */
class DisableBaseCommand extends Command
{

    /** @var KnowledgeBaseRepository $repo  */
    private $repo;

    /** @var EntityManagerInterface $em */
    private $em;

    public function __construct(EntityManagerInterface $em, KnowledgeBaseRepository $repo)
    {
        $this->em = $em;
        $this->repo = $repo;

        parent::__construct();
    }

    /**
     * @inheritdoc
     */
    protected function configure()
    {
        $this 
            ->setName('base:disable')
            ->setDescription('Disables a knowledge base, by default disables a whole group by bind id')
            ->addOption('single', 's', InputOption::VALUE_NONE, 'Will update only a single base revision (id acts as a base id not a bind id)')
            ->addArgument('id', InputArgument::REQUIRED, 'Either base id or bind id depending on the single option');
    }   

    /**
     * @inheritdoc
     */
    protected function execute(InputInterface $input, OutputInterface $output)
    {
        $id = intval($input->getArgument('id'));
        $single = !!$input->getOption('single');
        $bases = $single ? [$this->repo->getById($id)] : $this->repo->getByBindId($id);

        /** @var KnowledgeBase $base */
        foreach($bases as $base)
        {
            $base->setStatus(KnowledgeBase::STATUS_DISABLED);
        }

        $this->repo->clearCache();
        $this->em->flush();
        
        return 0;
    }

    
}


