<?php

namespace App\Command\Migration;

use App\Entity\Client;
use App\Entity\HardwareProfile;
use App\Entity\OperativeSystem;
use App\Entity\OrganizationalUnit;
use App\Entity\Partition;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\Query\ResultSetMapping;
use Doctrine\Persistence\ManagerRegistry;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

#[AsCommand(name: 'opengnsys:migration:partition', description: 'Migrate os data')]
class MigratePartitionClientCommand extends Command
{
    public function __construct(
        private readonly EntityManagerInterface      $entityManager,
        private readonly ManagerRegistry             $doctrine
    )
    {
        parent::__construct();
    }

    protected function execute(InputInterface $input, OutputInterface $output): int
    {
        ini_set('memory_limit', '512M');

        /** @var EntityManagerInterface $oldDatabaseEntityManager */
        $oldDatabaseEntityManager = $this->doctrine->getManager('og_1');

        $clientRepository = $this->entityManager->getRepository(Client::class);
        $operativeSystemRepository = $this->entityManager->getRepository(OperativeSystem::class);

        /** Obtener las particiones de los clientes de la base de datos antigua **/
        $rsmPartitions = new ResultSetMapping();
        $rsmPartitions->addScalarResult('idordenador', 'idordenador');
        $rsmPartitions->addScalarResult('idnombreso', 'idnombreso');
        $rsmPartitions->addScalarResult('numdisk', 'numdisk');
        $rsmPartitions->addScalarResult('numpar', 'numpar');
        $rsmPartitions->addScalarResult('codpar', 'codpar');
        $rsmPartitions->addScalarResult('tamano', 'tamano');
        $rsmPartitions->addScalarResult('uso', 'uso');

        $partitionQuery = $oldDatabaseEntityManager->createNativeQuery('SELECT idordenador, idnombreso, numdisk, numpar, codpar, tamano, uso FROM ordenadores_particiones', $rsmPartitions);
        $partitions = $partitionQuery->getResult();

        /** Particiones **/
        $output->writeln("PARTICIONES TOTAL: ". count($partitions));
        foreach ($partitions as $partition){
            $clientEntity = $clientRepository->findOneBy(['migrationId' => $partition['idordenador']]);
            if(!$clientEntity){
                $output->writeln("No se ha encontrado el cliente con id: ". $partition['idordenador']);
                continue;
            }

            $operativeSystemEntity = $operativeSystemRepository->findOneBy(['migrationId' => $partition['idnombreso']]);
            if(!$operativeSystemEntity){
                $output->writeln("No se ha encontrado el sistema operativo con id: ". $partition['idnombreso']);
                continue;
            }

            $partitionEntity = new Partition();
            $partitionEntity->setDiskNumber($partition['numdisk']);
            $partitionEntity->setPartitionNumber($partition['numpar']);
            $partitionEntity->setPartitionCode($partition['codpar']);
            $partitionEntity->setSize($partition['tamano']);
            $partitionEntity->setMemoryUsage($partition['uso']);
            $partitionEntity->setClient($clientEntity);
            $partitionEntity->setOperativeSystem($operativeSystemEntity);

            $this->entityManager->persist($partitionEntity);
            $this->entityManager->flush();
        }
        return 1;
    }
}