<?php

namespace App\Command;

use App\Bundle\Infoclinica\Rest;
use App\Entity\Idoctor;
use App\Entity\Department;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
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 Symfony\Component\DependencyInjection\ContainerInterface;

class UploadDoctorsInfoclinicaCommand extends Command
{
    protected static $defaultName = 'upload:doctorsInfoclinica';
    protected static $defaultDescription = 'Обновление и загрузка врачей из инфоклиники';
    protected $entityManager = null;
    protected $io = null;

    public function __construct(ContainerInterface $container)
    {
        parent::__construct();
        $this->entityManager = $container->get('doctrine')->getManager();
    }

    protected function configure(): void
    {
        $this->setDescription(self::$defaultDescription)
            ->addArgument('onlineMode', InputArgument::OPTIONAL, 'onlineMode [0,1]')
        ;
    }

    protected function execute(InputInterface $input, OutputInterface $output): int
    {
        $this->io = new SymfonyStyle($input, $output);
        $infoclinica = new Rest();
        $onlineMode = $input->getArgument('onlineMode');

        foreach ($this->entityManager->getRepository(Department::class)->findAll() as $department) {
            foreach ($infoclinica->getSpecialists($department->getDid(), $onlineMode)['response']['data'] as $doctor) {
                $iDoctor = $this->entityManager->getRepository(Idoctor::class)
                    ->findOneBy([
                        'dcode' => $doctor['dcode'],
                        'departmentId' => $doctor['departmentId'],
                        'onlineMode' => $onlineMode
                    ]);

                if (! $iDoctor) {
                    $this->io->success('create in specialist '. $doctor['dcode']);
                    $iDoctor = new Idoctor();
                } else {
                    $this->io->success('upgrade in specialist '. $doctor['dcode']);
                }

                $iDoctor
                    ->setDcode($doctor['dcode'])
                    ->setName($doctor['name'])
                    ->setDepartmentId($doctor['departmentId'])
                    ->setDepartmentName($doctor['departmentName'])
                    ->setFilialId($doctor['filialId'])
                    ->setFilialName($doctor['filialName'])
                    ->setComment(empty($doctor['comment'])? '':$doctor['comment'])
                    ->setNearestDate($doctor['nearestDate'])
                    ->setViewInWeb($doctor['viewInWeb'])
                    ->setOnlineMode($onlineMode)
                ;

                $this->entityManager->persist($iDoctor);
            }

            $this->entityManager->flush();
        }

        return Command::SUCCESS;
    }
}
