<?php

declare(strict_types=1);

namespace DoctrineMigrations;

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

/**
 * Adds tenant_id to the UNIQUE constraint on teacher_assignments.
 *
 * The original constraint (teacher_id, school_class_id, subject_id, academic_year_id)
 * allowed the same tuple to exist across different tenants, which is correct,
 * but did not scope uniqueness per tenant. Including tenant_id ensures that
 * uniqueness is enforced within each tenant boundary.
 */
final class Version20260212200000 extends AbstractMigration
{
    public function getDescription(): string
    {
        return 'Add tenant_id to teacher_assignments UNIQUE constraint for multi-tenant safety';
    }

    public function up(Schema $schema): void
    {
        $this->addSql(<<<'SQL'
            ALTER TABLE teacher_assignments
                DROP CONSTRAINT IF EXISTS teacher_assignments_teacher_id_school_class_id_subject_id_acad_key
        SQL);

        $this->addSql(<<<'SQL'
            ALTER TABLE teacher_assignments
                ADD CONSTRAINT teacher_assignments_tenant_teacher_class_subject_year_key
                UNIQUE (tenant_id, teacher_id, school_class_id, subject_id, academic_year_id)
        SQL);
    }

    public function down(Schema $schema): void
    {
        $this->addSql(<<<'SQL'
            ALTER TABLE teacher_assignments
                DROP CONSTRAINT IF EXISTS teacher_assignments_tenant_teacher_class_subject_year_key
        SQL);

        $this->addSql(<<<'SQL'
            ALTER TABLE teacher_assignments
                ADD CONSTRAINT teacher_assignments_teacher_id_school_class_id_subject_id_acad_key
                UNIQUE (teacher_id, school_class_id, subject_id, academic_year_id)
        SQL);
    }
}
