<?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 Doctrine\ORM\EntityManagerInterface;
use NPS\Repository\KnowledgeBaseRepository;
use NPS\DependencyInjection\IKnowledgeBaseInfoProvider;

/**
 * Invalidates and loads DB cache of all knowledge bases
 * @author Lukas Korinek
 */
class InvalidateDBBaseCommand extends Command
{

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

    /** @var IKnowledgeBaseInfoProvider $provider */
    private $provider;

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

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

        parent::__construct();
    }

    /**
     * @inheritdoc
     */
    protected function configure()
    {
        $this 
            ->setName('base:dbinvalidate')
            ->setDescription('Invalidates and updates DB cache of knowledge bases');
    }   

    /**
     * @inheritdoc
     */
    protected function execute(InputInterface $input, OutputInterface $output)
    {   
        $bases = $this->repo->getAll();

        foreach($bases as $base)
        {
            $base->invalidateDbCache();

            // Load the specifics for every language
            foreach($base->getSupportedLanguages() as $language)
            {
                $this->provider->setDefaultLanguage($language);

                $base->getName();
                $base->getDescription();

            }
        }

        // Flush the loaded contents
        $this->em->flush();

        return 0;

    }
}


