<?php

declare(strict_types=1);

namespace DoctrineMigrations;

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

final class Version20260612132848 extends AbstractMigration
{
    public function getDescription(): string
    {
        return 'Add type, clinic_id to doctor_addresses; make doctor_id nullable to support clinic addresses';
    }

    public function up(Schema $schema): void
    {
        $this->addSql("ALTER TABLE doctor_addresses ADD COLUMN type VARCHAR(10) NOT NULL DEFAULT 'personal'");
        $this->addSql("ALTER TABLE doctor_addresses ADD COLUMN clinic_id INT NULL");
        $this->addSql("ALTER TABLE doctor_addresses ADD CONSTRAINT FK_doctor_addr_clinic FOREIGN KEY (clinic_id) REFERENCES clinics(id) ON DELETE CASCADE");
        $this->addSql("CREATE INDEX idx_doctor_addr_clinic ON doctor_addresses (clinic_id)");
        $this->addSql("ALTER TABLE doctor_addresses MODIFY COLUMN doctor_id INT NULL");
        $this->addSql("ALTER TABLE doctor_addresses ADD CONSTRAINT chk_addr_owner CHECK (doctor_id IS NOT NULL OR clinic_id IS NOT NULL)");
    }

    public function down(Schema $schema): void
    {
        $this->addSql("ALTER TABLE doctor_addresses DROP CONSTRAINT chk_addr_owner");
        $this->addSql("ALTER TABLE doctor_addresses MODIFY COLUMN doctor_id INT NOT NULL");
        $this->addSql("DROP INDEX idx_doctor_addr_clinic ON doctor_addresses");
        $this->addSql("ALTER TABLE doctor_addresses DROP FOREIGN KEY FK_doctor_addr_clinic");
        $this->addSql("ALTER TABLE doctor_addresses DROP COLUMN clinic_id");
        $this->addSql("ALTER TABLE doctor_addresses DROP COLUMN type");
    }
}
