-- ============================================================
-- Migration 005: taught_lessons nullable class_id
--               + class_id FK on teacher_timetable_slots
-- Run after: 004_extended_schema.sql
-- ============================================================

-- taught_lessons.class_id: allow null so slots without a matched class can still materialize
ALTER TABLE taught_lessons ALTER COLUMN class_id DROP NOT NULL;

-- teacher_timetable_slots: add proper class FK alongside existing subject_class text
ALTER TABLE teacher_timetable_slots
    ADD COLUMN IF NOT EXISTS class_id UUID REFERENCES classes(id) ON DELETE SET NULL;

CREATE INDEX IF NOT EXISTS idx_tl_class_id    ON taught_lessons(class_id) WHERE class_id IS NOT NULL;
CREATE INDEX IF NOT EXISTS idx_tts_class_id   ON teacher_timetable_slots(class_id) WHERE class_id IS NOT NULL;
