<?php

namespace App\Command;

use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

use Doctrine\ORM\EntityManagerInterface;

use App\Repository\BroadcastRepository;

use App\Entity\State;

#[AsCommand(
    name: 'investigation:broadcast',
    description: 'Add a short description for your command',
)]
class InvestigationBroadcastCommand extends Command
{
    /**
     * @param EntityManagerInterface $em
     * @param BroadcastRepository $broadcastRepository
     */
    public function __construct(
        private EntityManagerInterface $em,
        private BroadcastRepository $broadcastRepository,
    ) {
        parent::__construct();
        $this->em = $em;
        $this->broadcastRepository = $broadcastRepository;
    }

    protected function execute(InputInterface $input, OutputInterface $output): int
    {
        $broadcasts = $this->broadcastRepository->findBroadcast();

        foreach ($broadcasts as $broadcast) {
            $state = new State();
            $state->setBroadcast($broadcast);
            $state->setInvestigation($broadcast->getInvestigation());
            $state->setProgress(2);
            $state->setStep(3);
            $this->em->persist($state);
            $this->em->flush();


            $broadcast->setLastState(32);
            $this->em->persist($broadcast);
            $this->em->flush();

            $output->writeln('<info>'.$broadcast->getInvestigation()->getId().'</info> Investigation Broadcast');
        }

        
        return Command::SUCCESS;
    }
}
