<?php

declare(strict_types=1);

namespace DoctrineMigrations;

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

/**
 * Blog posts get an optional city so the multi-domain public site can scope them.
 * NULL is a permanent, meaningful state: a nationwide post, canonical on the main
 * domain and listed on every city domain.
 *
 * Existing rows become nationwide, which preserves current behaviour.
 *
 * Note: doctrine:migrations:diff also wanted to alter date_overrides,
 * patient_sessions, wallet_transactions and weekly_schedules. That is
 * pre-existing drift between the entities and the database, unrelated to this
 * feature, so it is deliberately left out rather than bundled in here.
 */
final class Version20260719044321 extends AbstractMigration
{
    public function getDescription(): string
    {
        return 'Add nullable blogs.city_id (NULL = nationwide post)';
    }

    public function up(Schema $schema): void
    {
        $this->addSql('ALTER TABLE blogs ADD city_id INT DEFAULT NULL');
        $this->addSql('ALTER TABLE blogs ADD CONSTRAINT FK_F41BCA708BAC62AF FOREIGN KEY (city_id) REFERENCES cities (id) ON DELETE SET NULL');
        $this->addSql('CREATE INDEX idx_blogs_city ON blogs (city_id)');
    }

    public function down(Schema $schema): void
    {
        $this->addSql('ALTER TABLE blogs DROP FOREIGN KEY FK_F41BCA708BAC62AF');
        $this->addSql('DROP INDEX idx_blogs_city ON blogs');
        $this->addSql('ALTER TABLE blogs DROP city_id');
    }
}
