<?php

declare(strict_types=1);

namespace BeyondSEODeps\Doctrine\ORM\Tools\Console\Command\SchemaTool;

use BeyondSEODeps\Doctrine\ORM\Tools\Console\Command\AbstractEntityManagerCommand;
use BeyondSEODeps\Doctrine\ORM\Tools\Console\CommandCompatibility;
use BeyondSEODeps\Doctrine\ORM\Tools\SchemaTool;
use BeyondSEODeps\Symfony\Component\Console\Input\InputInterface;
use BeyondSEODeps\Symfony\Component\Console\Output\OutputInterface;
use BeyondSEODeps\Symfony\Component\Console\Style\SymfonyStyle;

/**
 * Base class for CreateCommand, DropCommand and UpdateCommand.
 *
 * @link    www.doctrine-project.org
 */
abstract class AbstractCommand extends AbstractEntityManagerCommand
{
    use CommandCompatibility;

    /**
     * @param mixed[] $metadatas
     *
     * @return int|null Null or 0 if everything went fine, or an error code.
     */
    abstract protected function executeSchemaCommand(InputInterface $input, OutputInterface $output, SchemaTool $schemaTool, array $metadatas, SymfonyStyle $ui);

    private function doConfigure(): void
    {
    }

    private function doExecute(InputInterface $input, OutputInterface $output): int
    {
        $ui = new SymfonyStyle($input, $output);

        $em = $this->getEntityManager($input);

        $metadatas = $em->getMetadataFactory()->getAllMetadata();

        if (empty($metadatas)) {
            $ui->getErrorStyle()->success('No Metadata Classes to process.');

            return 0;
        }

        return $this->executeSchemaCommand($input, $output, new SchemaTool($em), $metadatas, $ui);
    }
}
