<?php

declare(strict_types=1);

namespace App\Module\Core\Infrastructure\Console;

use App\Shared\Infrastructure\Database\ColumnCommentsCatalog;
use Doctrine\DBAL\Connection;
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:apply-column-comments',
    description: 'Reapplique les COMMENT ON TABLE/COLUMN du catalogue (workaround schema:update).',
)]
final class ApplyColumnCommentsCommand extends Command
{
    public function __construct(
        private readonly Connection $connection,
    ) {
        parent::__construct();
    }

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

        $statements = ColumnCommentsCatalog::toSqlStatements();

        foreach ($statements as $sql) {
            $this->connection->executeStatement($sql);
        }

        $io->success(sprintf('%d COMMENT ON statements appliques.', count($statements)));

        return Command::SUCCESS;
    }
}
