<?php

declare(strict_types=1);

namespace DoctrineMigrations;

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

final class Version20260302091704 extends AbstractMigration
{
    public function getDescription(): string
    {
        return 'Créer la table schedule_slots pour l\'emploi du temps';
    }

    public function up(Schema $schema): void
    {
        $this->addSql(<<<'SQL'
            CREATE TABLE schedule_slots (
                id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
                tenant_id UUID NOT NULL,
                class_id UUID NOT NULL,
                subject_id UUID NOT NULL,
                teacher_id UUID NOT NULL,
                day_of_week SMALLINT NOT NULL CHECK (day_of_week BETWEEN 1 AND 7),
                start_time VARCHAR(5) NOT NULL,
                end_time VARCHAR(5) NOT NULL,
                room VARCHAR(50),
                is_recurring BOOLEAN NOT NULL DEFAULT true,
                created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
                updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
                CONSTRAINT valid_times CHECK (end_time > start_time),
                CONSTRAINT fk_schedule_class FOREIGN KEY (class_id) REFERENCES school_classes(id) ON DELETE CASCADE,
                CONSTRAINT fk_schedule_subject FOREIGN KEY (subject_id) REFERENCES subjects(id) ON DELETE CASCADE,
                CONSTRAINT fk_schedule_teacher FOREIGN KEY (teacher_id) REFERENCES users(id) ON DELETE CASCADE
            )
        SQL);

        $this->addSql('CREATE INDEX idx_schedule_tenant ON schedule_slots(tenant_id)');
        $this->addSql('CREATE INDEX idx_schedule_class ON schedule_slots(class_id)');
        $this->addSql('CREATE INDEX idx_schedule_teacher ON schedule_slots(teacher_id)');
        $this->addSql('CREATE INDEX idx_schedule_day ON schedule_slots(day_of_week)');
        $this->addSql('CREATE INDEX idx_schedule_teacher_day ON schedule_slots(tenant_id, teacher_id, day_of_week)');
        $this->addSql('CREATE INDEX idx_schedule_room_day ON schedule_slots(tenant_id, room, day_of_week) WHERE room IS NOT NULL');
    }

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