<?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 Version20260723171254 extends AbstractMigration
{
    public function getDescription(): string
    {
        return 'Add medical-review gate fields to blogs (sources, review_status, reviewer, reviewed_at, review_note, topic_slug)';
    }

    // MariaDB auto-commits on DDL, so wrapping this migration in a transaction only
    // creates savepoints that the first ALTER destroys ("SAVEPOINT DOCTRINE_2 does
    // not exist"). Run it outside a transaction; the statements are idempotent.
    public function isTransactional(): bool
    {
        return false;
    }

    public function up(Schema $schema): void
    {
        // Scoped to the blogs table only. Unrelated project-wide schema drift that
        // doctrine:diff also emitted (messenger_messages, date_overrides, …) is
        // deliberately left out — it belongs to other migrations, not this feature.
        // Idempotent (MariaDB IF [NOT] EXISTS): a prior failed --all-or-nothing run
        // can leave columns applied while the version record was rolled back, so a
        // re-run must heal a partial state instead of erroring on "duplicate column".
        // Nullable columns first (safe on a table with existing rows).
        $this->addSql('ALTER TABLE blogs ADD COLUMN IF NOT EXISTS review_status VARCHAR(20) DEFAULT NULL, ADD COLUMN IF NOT EXISTS reviewed_at INT DEFAULT NULL, ADD COLUMN IF NOT EXISTS review_note VARCHAR(500) DEFAULT NULL, ADD COLUMN IF NOT EXISTS topic_slug VARCHAR(255) DEFAULT NULL, ADD COLUMN IF NOT EXISTS reviewer_id INT DEFAULT NULL');
        // `sources` is JSON NOT NULL: adding it directly to a table that already has
        // rows fails the json_valid CHECK (empty backfill is invalid JSON).
        // Add nullable, backfill '[]', then enforce NOT NULL.
        $this->addSql('ALTER TABLE blogs ADD COLUMN IF NOT EXISTS sources JSON DEFAULT NULL');
        $this->addSql("UPDATE blogs SET sources = '[]' WHERE sources IS NULL");
        $this->addSql('ALTER TABLE blogs MODIFY sources JSON NOT NULL');
        $this->addSql('ALTER TABLE blogs DROP FOREIGN KEY IF EXISTS FK_F41BCA7070574616');
        $this->addSql('ALTER TABLE blogs ADD CONSTRAINT FK_F41BCA7070574616 FOREIGN KEY (reviewer_id) REFERENCES users (id) ON DELETE SET NULL');
        $this->addSql('CREATE UNIQUE INDEX IF NOT EXISTS UNIQ_F41BCA70A076FAD7 ON blogs (topic_slug)');
        $this->addSql('CREATE INDEX IF NOT EXISTS IDX_F41BCA7070574616 ON blogs (reviewer_id)');
        $this->addSql('CREATE INDEX IF NOT EXISTS idx_blogs_review_status ON blogs (review_status, created_at)');
    }

    public function down(Schema $schema): void
    {
        $this->addSql('ALTER TABLE blogs DROP FOREIGN KEY FK_F41BCA7070574616');
        $this->addSql('DROP INDEX UNIQ_F41BCA70A076FAD7 ON blogs');
        $this->addSql('DROP INDEX IDX_F41BCA7070574616 ON blogs');
        $this->addSql('DROP INDEX idx_blogs_review_status ON blogs');
        $this->addSql('ALTER TABLE blogs DROP sources, DROP review_status, DROP reviewed_at, DROP review_note, DROP topic_slug, DROP reviewer_id');
    }
}
