<?php 

namespace NPS\DataFixtures;

use Doctrine\Bundle\FixturesBundle\Fixture;
use Doctrine\Common\Persistence\ObjectManager;
use NPS\Entity\UserEntity;
use NPS\Factory\UserFactory;
use Doctrine\ORM\EntityManagerInterface;
use NPS\Entity\UserQuestionQueryEntity;
use NPS\Entity\ErrorReportingQueryEntity;
use NPS\Factory\KnowledgeBaseFactory;
use NPS\Repository\KnowledgeBaseRepository;
use NPS\Model\KnowledgeBase;
use NPS\Entity\ConsultationEntity;

/**
 * Application data fixtures definition
 * @author Lukas Korinek
 */
class AppFixtures extends Fixture
{

    /** @var UserFactory $userFactory */
    private $userFactory;

    /** @var KnowledgeBaseFactory $baseFactory */
    private $baseFactory;

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

    /** @var string $rootDir */
    private $rootDir;

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

    public function __construct(UserFactory $userFactory, KnowledgeBaseRepository $baseRepo, KnowledgeBaseFactory $baseFactory, EntityManagerInterface $em, $rootDir)
    {
        $this->userFactory = $userFactory;
        $this->baseFactory = $baseFactory;
        $this->baseRepo = $baseRepo;
        $this->em = $em;
        $this->rootDir = $rootDir;
    }

    /**
     * @inheritdoc  
     */
    public function load(ObjectManager $manager)
    {
        $user1 = $this->userFactory->create('auth@nps', 'Authtest user', 'testAJC48', ['ROLE_USER']);
        $user1->activate();
        $manager->persist($user1->getEntity());
        $manager->flush();


        $user2 = $this->userFactory->create('authadmin@nps', 'Authtest admin', 'testAJC48', ['ROLE_ADMIN']);
        $user2->activate();
        $manager->persist($user2->getEntity());
        $manager->flush();

        // Question
        $question = UserQuestionQueryEntity::create([
            'email' => 'user@nps',
            'text' => 'a question'
        ]);
        $manager->persist($question);

        // Report
        $report = ErrorReportingQueryEntity::create([
            'email' => 'user@nps',
            'text' => 'some error',
        ]);
        $manager->persist($report);
        $manager->flush();

        // Knowledge base
        $path = $this->rootDir . '/../tests/Knowledge/test.nps';
        $content = file_get_contents($path);
        $this->baseRepo->clearCache();
        $base = $this->baseFactory->create('Notebooky', $content, 99999);
        $base->setStatus(KnowledgeBase::STATUS_DEMO);
        $manager->persist($base->getEntity());
        $manager->flush();
        $this->baseRepo->clearCache();

        // Consultation
        $consultation = ConsultationEntity::create([
            'flagged' => false,
            'user' => $user1->getEntity(),
            'base' => $base->getEntity(),
            'state' => ''
        ]);
        $manager->persist($consultation);
        $manager->flush();

        $user1->grantAccess($base->getKnowledgeBaseBindId());
        $manager->flush();
        
    }
}

