<?php

declare(strict_types=1);

namespace DoctrineMigrations;

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

final class Version20260304103700 extends AbstractMigration
{
    public function getDescription(): string
    {
        return 'Story 4.2 : Récurrences hebdomadaires — ajout bornes récurrence et table exceptions';
    }

    public function up(Schema $schema): void
    {
        // Ajout des bornes de récurrence sur schedule_slots
        $this->addSql('ALTER TABLE schedule_slots ADD COLUMN recurrence_start DATE');
        $this->addSql('ALTER TABLE schedule_slots ADD COLUMN recurrence_end DATE');

        // Table des exceptions (modifications/annulations ponctuelles)
        $this->addSql('
            CREATE TABLE schedule_exceptions (
                id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
                tenant_id UUID NOT NULL,
                slot_id UUID NOT NULL REFERENCES schedule_slots(id) ON DELETE CASCADE,
                exception_date DATE NOT NULL,
                exception_type VARCHAR(20) NOT NULL,
                new_start_time VARCHAR(5),
                new_end_time VARCHAR(5),
                new_room VARCHAR(50),
                new_teacher_id UUID REFERENCES users(id),
                reason TEXT,
                created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
                created_by UUID NOT NULL REFERENCES users(id),
                UNIQUE (slot_id, exception_date)
            )
        ');

        $this->addSql('CREATE INDEX idx_exceptions_slot ON schedule_exceptions(slot_id)');
        $this->addSql('CREATE INDEX idx_exceptions_date ON schedule_exceptions(exception_date)');
        $this->addSql('CREATE INDEX idx_exceptions_tenant ON schedule_exceptions(tenant_id)');
    }

    public function down(Schema $schema): void
    {
        $this->addSql('DROP TABLE IF EXISTS schedule_exceptions');
        $this->addSql('ALTER TABLE schedule_slots DROP COLUMN IF EXISTS recurrence_start');
        $this->addSql('ALTER TABLE schedule_slots DROP COLUMN IF EXISTS recurrence_end');
    }
}
