<?php

declare(strict_types=1);

namespace MauticPlugin\DealFlowBundle\Migrations;

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

final class Version_0_3_0 extends AbstractMigration
{
    protected function isApplicable(Schema $schema): bool
    {
        return !$schema->hasTable($this->concatPrefix(InboundWebhookSource::TABLE_NAME));
    }

    protected function up(): void
    {
        $this->entityManager->getConnection()->executeStatement(sprintf(
            'CREATE TABLE IF NOT EXISTS `%s` (
                `id` INT AUTO_INCREMENT NOT NULL,
                `name` VARCHAR(191) NOT NULL,
                `source_key` VARCHAR(64) NOT NULL,
                `secret` VARCHAR(191) NOT NULL,
                `is_active` TINYINT(1) NOT NULL DEFAULT 1,
                `last_used_at` DATETIME DEFAULT NULL,
                `created_at` DATETIME NOT NULL,
                `updated_at` DATETIME NOT NULL,
                UNIQUE INDEX `dealflow_inbound_webhook_source_key_unique` (`source_key`),
                INDEX `dealflow_inbound_webhook_active_idx` (`is_active`),
                PRIMARY KEY(`id`)
            ) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_unicode_ci` ENGINE = InnoDB',
            $this->concatPrefix(InboundWebhookSource::TABLE_NAME)
        ));
    }
}
