<?php

namespace WordpressModels\ORM\Migrations;

use Doctrine\DBAL\Platforms\MySQL80Platform;
use Doctrine\DBAL\Schema\MySQLSchemaManager;
use Doctrine\Migrations\Configuration\Connection\ExistingConnection;
use Doctrine\Migrations\DependencyFactory;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\Mapping\Driver\DatabaseDriver;
use WordpressModels\ORM\Command\DatabaseSchemaTool;

class MigrationsTool
{

    public function __construct(private EntityManagerInterface $em)
    {
        $tool = new DatabaseSchemaTool($em, $this->getEnhancedSchemaManager($em));
    }

    /**
     * @return string[]
     * @throws \Doctrine\DBAL\Exception
     */
    public function getMigrationLines(): array
    {
        $dependencyFactory = DependencyFactory::fromEntityManager(
            $this->em,
            new DatabaseDriver($schemaManager),
            new ExistingConnection($connection),
        );

        $schemaDumper = $dependencyFactory->getSchemaDumper();
        $nameGenerator = $dependencyFactory->getClassNameGenerator();
        $className = $nameGenerator->generateClassName($metadata);
        $schemaDumper->dump('App\Migrations\Version20211010101010', [], true, 120);
    }

    /**
     * Get an enhanced schema manager that can handle the enum type.
     *
     * @param EntityManagerInterface $entityManager
     * @return DatabaseDriver
     * @throws \Doctrine\DBAL\Exception
     */
    private function getEnhancedSchemaManager(EntityManagerInterface $entityManager): MySQLSchemaManager
    {
        $platform = new MySQL80Platform();
        $platform->registerDoctrineTypeMapping('enum', 'string');

        return new MySQLSchemaManager($entityManager->getConnection(), $platform);
    }



}