<?php

declare(strict_types=1);

namespace DoctrineMigrations;

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

/**
 * Migration pour la table school_classes.
 *
 * @see Story 2.1 - Création et Gestion des Classes
 * @see FR73: Organiser les élèves par groupes pédagogiques
 */
final class Version20260204100000 extends AbstractMigration
{
    public function getDescription(): string
    {
        return 'Create school_classes table (Story 2.1)';
    }

    public function up(Schema $schema): void
    {
        $this->addSql(<<<'SQL'
            CREATE TABLE school_classes (
                id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
                tenant_id UUID NOT NULL,
                school_id UUID NOT NULL,
                academic_year_id UUID NOT NULL,
                name VARCHAR(50) NOT NULL,
                level VARCHAR(50),
                capacity INT,
                status VARCHAR(20) NOT NULL DEFAULT 'active',
                description TEXT,
                created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
                updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
                deleted_at TIMESTAMPTZ NULL,
                UNIQUE(tenant_id, academic_year_id, name)
            )
        SQL);

        // Index pour les requêtes fréquentes
        $this->addSql('CREATE INDEX idx_school_classes_tenant_id ON school_classes(tenant_id)');
        $this->addSql('CREATE INDEX idx_school_classes_academic_year ON school_classes(academic_year_id)');
        $this->addSql('CREATE INDEX idx_school_classes_status ON school_classes(status)');
        $this->addSql('CREATE INDEX idx_school_classes_school ON school_classes(school_id)');

        // Index composite pour les requêtes de liste par tenant et année
        $this->addSql('CREATE INDEX idx_school_classes_tenant_year_status ON school_classes(tenant_id, academic_year_id, status)');

        $this->addSql("COMMENT ON TABLE school_classes IS 'Classes scolaires organisées par année académique et établissement (FR73)'");
    }

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