<?php

namespace App\Command;
use App\Entity\Question;
use App\Entity\QuestionType;
use App\Entity\User;
use App\Entity\UserType;
use App\Repository\UserTypeRepository;
use App\Utils\Utils;
use Doctrine\DBAL\Connection;
use Doctrine\ORM\EntityManagerInterface;
use Psr\Container\ContainerInterface;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\HttpKernel\KernelInterface;


/**
 * Command to import excel that with answers of specifications
 *
 * Class CmdImportAll
 * @package App\Command
 */
class CmdDeleteUser extends Command
{
    protected static $defaultName = 'app:delete-user';

    private $em;
    private $container;
    private $kernel;

    public function __construct(EntityManagerInterface $em, ContainerInterface $container, KernelInterface $kernel)
    {
        $this->em = $em;
        $this->container = $container;
        $this->kernel = $kernel;
        parent::__construct();
    }

    /**
     *
     */
    protected function configure()
    {
        $this
            // the short description shown while running "php bin/console list"
            ->setDescription('Delete user.')

            // the full command description shown when running the command with
            // the "--help" option
            ->setHelp('This command allows you to delete an user.')
            ->addArgument('id', InputArgument::REQUIRED, 'id')
        ;
    }

    /**
     * @param InputInterface $input
     * @param OutputInterface $output
     * @return int|null|void
     * @throws \Exception
     */
    protected function execute(InputInterface $input, OutputInterface $output)
    {
        $id = $input->getArgument('id');
        /** @var User $user */
        $user = $this->em->getRepository(User::class)->find($id);
        if ($user === null) {
            throw new \Exception('user not found');
        }
        /** @var UserType $userType */
        $userType = $user->getUserType();
        if ($userType->getType() === UserTypeRepository::USER_TYPE_SYSTEM) {
            throw new \Exception('can no delete system user');
        }

        $this->em->remove($user);
        $this->em->flush();
        return Command::SUCCESS;
    }

}