<?php

declare(strict_types=1);

namespace DoctrineMigrations;

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

/**
 * Migration pour créer la table subjects (matières).
 *
 * @see Story 2.2 - Création et Gestion des Matières
 * @see FR74 - Structurer l'offre pédagogique
 */
final class Version20260205100001 extends AbstractMigration
{
    public function getDescription(): string
    {
        return 'Create subjects table for school subjects management';
    }

    public function up(Schema $schema): void
    {
        $this->addSql(<<<'SQL'
            CREATE TABLE subjects (
                id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
                tenant_id UUID NOT NULL,
                school_id UUID NOT NULL,
                name VARCHAR(100) NOT NULL,
                code VARCHAR(10) NOT NULL,
                color VARCHAR(7),
                description TEXT,
                status VARCHAR(20) NOT NULL DEFAULT 'active',
                created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
                updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
                deleted_at TIMESTAMPTZ NULL
            )
        SQL);

        // Index pour la recherche par tenant et school
        $this->addSql('CREATE INDEX idx_subjects_tenant_id ON subjects(tenant_id)');
        $this->addSql('CREATE INDEX idx_subjects_school_id ON subjects(school_id)');

        // Index unique partiel pour l'unicité du code par tenant/school (excluant les matières archivées)
        $this->addSql(<<<'SQL'
            CREATE UNIQUE INDEX idx_subjects_unique_code_active
            ON subjects (tenant_id, school_id, code)
            WHERE deleted_at IS NULL
        SQL);
    }

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