<?php

declare(strict_types=1);

namespace DoctrineMigrations;

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

/**
 * Doctor profile ownership for IRIMC (سازمان نظام پزشکی) import.
 *
 * Adds owner_status / source / source_ref / managed_by / claimed_at to `doctors`
 * so imported doctors can be stored as "unclaimed" (managed by a system user)
 * and later transferred to the real doctor. Existing rows become manual+claimed
 * so their behavior is unchanged.
 */
final class Version20260711120000 extends AbstractMigration
{
    public function getDescription(): string
    {
        return 'Doctor profile ownership (owner_status, source, source_ref, managed_by, claimed_at) for IRIMC import';
    }

    public function up(Schema $schema): void
    {
        $this->addSql(<<<'SQL'
            ALTER TABLE doctors
                ADD owner_status VARCHAR(20) NOT NULL DEFAULT 'claimed',
                ADD source       VARCHAR(20) NOT NULL DEFAULT 'manual',
                ADD source_ref   VARCHAR(100) DEFAULT NULL,
                ADD managed_by   INT DEFAULT NULL,
                ADD claimed_at   INT DEFAULT NULL
        SQL);

        // فیلترِ «پزشکان بدون‌مالک» و تطبیق idempotency ایمپورت بر پایهٔ (source, medical_system_code)
        $this->addSql('CREATE INDEX idx_doctors_owner ON doctors (owner_status)');
        $this->addSql('CREATE INDEX idx_doctors_source ON doctors (source, medical_system_code)');
    }

    public function down(Schema $schema): void
    {
        $this->addSql('DROP INDEX idx_doctors_source ON doctors');
        $this->addSql('DROP INDEX idx_doctors_owner ON doctors');
        $this->addSql(<<<'SQL'
            ALTER TABLE doctors
                DROP owner_status,
                DROP source,
                DROP source_ref,
                DROP managed_by,
                DROP claimed_at
        SQL);
    }
}
