<?php

declare(strict_types=1);

namespace DoctrineMigrations;

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

/**
 * Creates the teacher_assignments table for Story 2.8.
 *
 * Models the ternary relationship Teacher × Class × Subject, scoped by
 * academic year and tenant. Supports multi-subject per teacher and
 * co-teaching (multiple teachers per subject/class).
 */
final class Version20260212100000 extends AbstractMigration
{
    public function getDescription(): string
    {
        return 'Create teacher_assignments table (Story 2.8)';
    }

    public function up(Schema $schema): void
    {
        $this->addSql(<<<'SQL'
            CREATE TABLE IF NOT EXISTS teacher_assignments (
                id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
                tenant_id UUID NOT NULL,
                teacher_id UUID NOT NULL,
                school_class_id UUID NOT NULL,
                subject_id UUID NOT NULL,
                academic_year_id UUID NOT NULL,
                start_date TIMESTAMPTZ NOT NULL DEFAULT NOW(),
                end_date TIMESTAMPTZ,
                status VARCHAR(20) NOT NULL DEFAULT 'active',
                created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
                updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
                UNIQUE(teacher_id, school_class_id, subject_id, academic_year_id)
            )
        SQL);

        $this->addSql('CREATE INDEX idx_teacher_assignments_tenant ON teacher_assignments(tenant_id)');
        $this->addSql('CREATE INDEX idx_teacher_assignments_teacher ON teacher_assignments(teacher_id)');
        $this->addSql('CREATE INDEX idx_teacher_assignments_class ON teacher_assignments(school_class_id)');
        $this->addSql('CREATE INDEX idx_teacher_assignments_year ON teacher_assignments(academic_year_id)');
        $this->addSql('CREATE INDEX idx_teacher_assignments_teacher_tenant_status ON teacher_assignments(teacher_id, tenant_id, status)');
        $this->addSql('CREATE INDEX idx_teacher_assignments_class_tenant_status ON teacher_assignments(school_class_id, tenant_id, status)');
    }

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