<?php

namespace App\Command;

use App\Entity\SnipContent;
use App\Repository\SnipContentRepository;
use App\Service\SnipContent\Lexer;
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 Symfony\Component\Console\Style\SymfonyStyle;

#[AsCommand(
    name: 'app:snip:update-content',
    description: 'Update Snip content line endings',
)]
class SnipUpdateContentCommand extends Command
{
    public function __construct(
        private readonly SnipContentRepository $snipContentRepository,
    )
    {
        parent::__construct();
    }

    protected function execute(InputInterface $input, OutputInterface $output): int
    {
//        $io = new SymfonyStyle($input, $output);

        $qb = $this->snipContentRepository->createQueryBuilder('s');
        $qb->where('s.text IS NOT NULL');

        /** @var SnipContent $snipContent */
        foreach ($qb->getQuery()->getResult() as $snipContent) {
            $text = $snipContent->text;
            $text = Lexer::reconstruct(Lexer::tokenize($text));
            $snipContent->text = $text;
            $this->snipContentRepository->save($snipContent);
        }

        return Command::SUCCESS;
    }
}
