<?php

declare(strict_types=1);

namespace DoctrineMigrations;

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

/**
 * Migration pour créer la table academic_periods (périodes scolaires).
 *
 * @see FR75 - Structurer l'année pour bulletins et moyennes
 */
final class Version20260205100002 extends AbstractMigration
{
    public function getDescription(): string
    {
        return 'Create academic_periods table for school period management';
    }

    public function up(Schema $schema): void
    {
        $this->addSql(<<<'SQL'
            CREATE TABLE academic_periods (
                id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
                tenant_id UUID NOT NULL,
                academic_year_id UUID NOT NULL,
                period_type VARCHAR(20) NOT NULL,
                sequence INT NOT NULL,
                label VARCHAR(20) NOT NULL,
                start_date DATE NOT NULL,
                end_date DATE NOT NULL,
                created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
                updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
            )
        SQL);

        $this->addSql('CREATE INDEX idx_academic_periods_tenant_id ON academic_periods(tenant_id)');
        $this->addSql('CREATE INDEX idx_academic_periods_year ON academic_periods(academic_year_id)');
        $this->addSql(<<<'SQL'
            CREATE UNIQUE INDEX idx_academic_periods_unique_sequence
            ON academic_periods (tenant_id, academic_year_id, sequence)
        SQL);
    }

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