<?php

namespace App\Command;

use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Helper\ProgressBar;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;

#[AsCommand(
    name: 'database:add:initiator',
    description: 'Set the initiator column for all messages',
    aliases: [],
    hidden: false,
)]
class AddInitiatorCommand extends Command
{
    private array $initiators = [];
    private array $parents = [];

    /**
     * TestAddinitiatorCommand constructor.
     */
    public function __construct(private readonly EntityManagerInterface $entityManager)
    {
        parent::__construct();
    }

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

        $io = new SymfonyStyle($input, $output);
        $io->block('Adding initiator values for conversations');

        $connection = $this->entityManager->getConnection();

        $count = $connection->fetchOne('SELECT count(*) from messages');

        $progress = new ProgressBar($output, $count);
        $progress->start();

        $statement = $connection->executeQuery('SELECT * FROM messages');

        for ($i = 0; $i < $count; ++$i) {
            $progress->advance();
            $row = $statement->fetchAssociative();

            $messageId = $row['id'];
            if ('30180' === $messageId) {
                continue;
            }
            if ('0' !== $row['initiator_id'] || null !== $row['initiator_id']) {
                $this->parents[$messageId] = $row['IdParent'];
            }
            if ('0' === $row['IdParent'] || null === $row['IdParent']) {
                $this->initiators[$messageId] = $row['IdSender'];
                $initiator = $row['IdSender'];
            } else {
                $this->parents[$messageId] = $row['IdParent'];
                $initiator = $this->getinitiator($messageId);
            }

            if (0 !== $initiator) {
                $connection->executeQuery(
                    'UPDATE messages m SET initiator_id = ' . $initiator . ' WHERE m.id = ' . $messageId
                );
            } else {
                $io->block('Nothing found for ' . $messageId);
            }
        }
        $progress->finish();

        return Command::SUCCESS;
    }

    private function getinitiator($parent)
    {
        while (isset($this->parents[$parent]) && '0' !== $this->parents[$parent]) {
            $parent = $this->parents[$parent];
        }

        $initiator = 0;
        if (isset($this->initiators[$parent])) {
            $initiator = (int) $this->initiators[$parent];
        }

        return $initiator;
    }
}
