<?php

declare(strict_types=1);

namespace DoctrineMigrations;

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

/**
 * جدول ممیزی درخواست‌های تصاحب پروفایل پزشک (claim).
 * دادهٔ حساس خام ذخیره نمی‌شود: کد ملی hash (sha256) و موبایل mask.
 */
final class Version20260711151000 extends AbstractMigration
{
    public function getDescription(): string
    {
        return 'doctor_claim_requests audit table for the profile-claim flow';
    }

    public function up(Schema $schema): void
    {
        $this->addSql(<<<'SQL'
            CREATE TABLE doctor_claim_requests (
                id INT AUTO_INCREMENT NOT NULL,
                uuid VARCHAR(36) NOT NULL,
                doctor_id INT NOT NULL,
                user_id INT DEFAULT NULL,
                status VARCHAR(20) NOT NULL,
                national_code_hash VARCHAR(64) NOT NULL,
                mobile_masked VARCHAR(15) NOT NULL,
                verification_method VARCHAR(40) NOT NULL,
                failure_reason VARCHAR(100) DEFAULT NULL,
                created_at INT NOT NULL,
                completed_at INT DEFAULT NULL,
                UNIQUE INDEX UNIQ_claim_uuid (uuid),
                INDEX idx_claim_doctor_status (doctor_id, status),
                INDEX IDX_claim_user (user_id),
                PRIMARY KEY(id)
            ) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_unicode_ci` ENGINE = InnoDB
        SQL);
        $this->addSql('ALTER TABLE doctor_claim_requests ADD CONSTRAINT FK_claim_doctor FOREIGN KEY (doctor_id) REFERENCES doctors (id) ON DELETE CASCADE');
        $this->addSql('ALTER TABLE doctor_claim_requests ADD CONSTRAINT FK_claim_user FOREIGN KEY (user_id) REFERENCES users (id) ON DELETE SET NULL');
    }

    public function down(Schema $schema): void
    {
        $this->addSql('DROP TABLE doctor_claim_requests');
    }
}
