<?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 Version20260723174500 extends AbstractMigration
{
    public function getDescription(): string
    {
        return 'Add SEO fields, scheduling (scheduled_at) and representative ownership (representation_id) to blogs';
    }

    // MariaDB auto-commits on DDL — run outside a transaction to avoid the broken
    // savepoint path. Statements are idempotent (IF [NOT] EXISTS) so re-runs heal.
    public function isTransactional(): bool
    {
        return false;
    }

    public function up(Schema $schema): void
    {
        // Scoped to the blogs table only. Idempotent (MariaDB IF [NOT] EXISTS) so a
        // re-run after a failed --all-or-nothing deploy heals a partial state rather
        // than erroring on duplicate columns/indexes.
        // Nullable columns + representation FK column.
        $this->addSql('ALTER TABLE blogs ADD COLUMN IF NOT EXISTS meta_title VARCHAR(255) DEFAULT NULL, ADD COLUMN IF NOT EXISTS meta_description VARCHAR(500) DEFAULT NULL, ADD COLUMN IF NOT EXISTS primary_keyword VARCHAR(255) DEFAULT NULL, ADD COLUMN IF NOT EXISTS reading_time INT DEFAULT NULL, ADD COLUMN IF NOT EXISTS canonical_url VARCHAR(500) DEFAULT NULL, ADD COLUMN IF NOT EXISTS og_image VARCHAR(500) DEFAULT NULL, ADD COLUMN IF NOT EXISTS scheduled_at INT DEFAULT NULL, ADD COLUMN IF NOT EXISTS representation_id INT DEFAULT NULL');
        // JSON NOT NULL columns can't be added directly to a table with rows (the
        // empty backfill fails json_valid). Add nullable, backfill '[]', then enforce.
        $this->addSql('ALTER TABLE blogs ADD COLUMN IF NOT EXISTS secondary_keywords JSON DEFAULT NULL, ADD COLUMN IF NOT EXISTS faq JSON DEFAULT NULL, ADD COLUMN IF NOT EXISTS internal_links JSON DEFAULT NULL, ADD COLUMN IF NOT EXISTS external_links JSON DEFAULT NULL');
        $this->addSql("UPDATE blogs SET secondary_keywords = '[]' WHERE secondary_keywords IS NULL");
        $this->addSql("UPDATE blogs SET faq = '[]' WHERE faq IS NULL");
        $this->addSql("UPDATE blogs SET internal_links = '[]' WHERE internal_links IS NULL");
        $this->addSql("UPDATE blogs SET external_links = '[]' WHERE external_links IS NULL");
        $this->addSql('ALTER TABLE blogs MODIFY secondary_keywords JSON NOT NULL, MODIFY faq JSON NOT NULL, MODIFY internal_links JSON NOT NULL, MODIFY external_links JSON NOT NULL');
        $this->addSql('ALTER TABLE blogs DROP FOREIGN KEY IF EXISTS FK_F41BCA7046CE82F4');
        $this->addSql('ALTER TABLE blogs ADD CONSTRAINT FK_F41BCA7046CE82F4 FOREIGN KEY (representation_id) REFERENCES representations (id) ON DELETE SET NULL');
        $this->addSql('CREATE INDEX IF NOT EXISTS IDX_F41BCA7046CE82F4 ON blogs (representation_id)');
        $this->addSql('CREATE INDEX IF NOT EXISTS idx_blogs_scheduled_at ON blogs (scheduled_at)');
        $this->addSql('CREATE INDEX IF NOT EXISTS idx_blogs_representation ON blogs (representation_id, created_at)');
    }

    public function down(Schema $schema): void
    {
        $this->addSql('ALTER TABLE blogs DROP FOREIGN KEY FK_F41BCA7046CE82F4');
        $this->addSql('DROP INDEX IDX_F41BCA7046CE82F4 ON blogs');
        $this->addSql('DROP INDEX idx_blogs_scheduled_at ON blogs');
        $this->addSql('DROP INDEX idx_blogs_representation ON blogs');
        $this->addSql('ALTER TABLE blogs DROP meta_title, DROP meta_description, DROP primary_keyword, DROP secondary_keywords, DROP faq, DROP internal_links, DROP external_links, DROP reading_time, DROP canonical_url, DROP og_image, DROP scheduled_at, DROP representation_id');
    }
}
