<?php

declare(strict_types=1);

namespace DoctrineMigrations;

use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;

/**
 * Auto-generated Migration: Please modify to your needs!
 */
final class Version20260611075829 extends AbstractMigration
{
    public function getDescription(): string
    {
        return '';
    }

    public function up(Schema $schema): void
    {
        // this up() migration is auto-generated, please modify it to your needs
        $this->addSql('CREATE TABLE user_active_context (db_uuid VARCHAR(36) NOT NULL, updated_at INT NOT NULL, user_id INT NOT NULL, PRIMARY KEY (user_id)) DEFAULT CHARACTER SET utf8mb4');
        $this->addSql('ALTER TABLE user_active_context ADD CONSTRAINT FK_A94E639A76ED395 FOREIGN KEY (user_id) REFERENCES users (id) ON DELETE CASCADE');
        // On a fresh DB site_config does not exist yet (it is created later by
        // Version20260611083424). IF EXISTS keeps this a no-op on fresh installs
        // while still dropping the legacy table on older drifted databases.
        $this->addSql('DROP TABLE IF EXISTS site_config');

        // appointments.booked_by / commission_rials are never created by any
        // migration — they only exist on older drifted dev DBs. Drop them only
        // if present so a fresh DB doesn't fail with errno 1091.
        $this->addSql('DROP PROCEDURE IF EXISTS __mig_075829');
        $this->addSql(<<<'SQL'
            CREATE PROCEDURE __mig_075829()
            BEGIN
                DECLARE db VARCHAR(64); SET db = DATABASE();
                IF EXISTS (SELECT 1 FROM information_schema.COLUMNS WHERE TABLE_SCHEMA = db AND TABLE_NAME = 'appointments' AND COLUMN_NAME = 'booked_by') THEN
                    ALTER TABLE appointments DROP booked_by;
                END IF;
                IF EXISTS (SELECT 1 FROM information_schema.COLUMNS WHERE TABLE_SCHEMA = db AND TABLE_NAME = 'appointments' AND COLUMN_NAME = 'commission_rials') THEN
                    ALTER TABLE appointments DROP commission_rials;
                END IF;
            END
        SQL);
        $this->addSql('CALL __mig_075829()');
        $this->addSql('DROP PROCEDURE __mig_075829');
    }

    public function down(Schema $schema): void
    {
        // this down() migration is auto-generated, please modify it to your needs
        $this->addSql('CREATE TABLE site_config (config_key VARCHAR(100) CHARACTER SET utf8mb4 NOT NULL COLLATE `utf8mb4_unicode_520_ci`, config_value LONGTEXT CHARACTER SET utf8mb4 DEFAULT NULL COLLATE `utf8mb4_unicode_520_ci`, updated_at INT NOT NULL, PRIMARY KEY (config_key)) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_unicode_520_ci` ENGINE = InnoDB COMMENT = \'\' ');
        $this->addSql('ALTER TABLE user_active_context DROP FOREIGN KEY FK_A94E639A76ED395');
        $this->addSql('DROP TABLE user_active_context');
        $this->addSql('ALTER TABLE appointments ADD booked_by VARCHAR(20) NOT NULL, ADD commission_rials INT DEFAULT NULL');
    }
}
