<?php

declare(strict_types=1);

namespace MauticPlugin\DealFlowBundle\Migrations;

use Doctrine\DBAL\Schema\Schema;
use Mautic\IntegrationsBundle\Migration\AbstractMigration;
use MauticPlugin\DealFlowBundle\Entity\DealField;

final class Version_0_6_2 extends AbstractMigration
{
    protected function isApplicable(Schema $schema): bool
    {
        $tableName = $this->concatPrefix(DealField::TABLE_NAME);

        return $schema->hasTable($tableName)
            && !$schema->getTable($tableName)->hasColumn('default_value');
    }

    protected function up(): void
    {
        $connection    = $this->entityManager->getConnection();
        $schemaManager = $connection->createSchemaManager();
        $tableName     = $this->concatPrefix(DealField::TABLE_NAME);
        $table         = $schemaManager->introspectTable($tableName);

        if (!$table->hasColumn('default_value')) {
            $connection->executeStatement(sprintf(
                'ALTER TABLE `%s` ADD `default_value` VARCHAR(191) DEFAULT NULL AFTER `options_json`',
                $tableName
            ));
        }
    }
}
