<?php

declare(strict_types=1);

namespace MauticPlugin\DealFlowBundle\Migrations;

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

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

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

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

        if (!$table->hasColumn('priority')) {
            $connection->executeStatement(sprintf(
                "ALTER TABLE `%s` ADD `priority` VARCHAR(32) NOT NULL DEFAULT 'normal'",
                $tableName
            ));
        }

        if (!$table->hasColumn('reminder_at')) {
            $connection->executeStatement(sprintf(
                'ALTER TABLE `%s` ADD `reminder_at` DATETIME DEFAULT NULL',
                $tableName
            ));
        }
    }
}
