-- Lessons are teaching content only (text, images, audio, video, thumbnail).
-- Question sets remain linked to practices, not lessons.

ALTER TABLE sub_module_lessons
    ADD COLUMN IF NOT EXISTS title VARCHAR(255),
    ADD COLUMN IF NOT EXISTS description TEXT,
    ADD COLUMN IF NOT EXISTS thumbnail TEXT,
    ADD COLUMN IF NOT EXISTS teaching_text TEXT,
    ADD COLUMN IF NOT EXISTS teaching_image_url TEXT,
    ADD COLUMN IF NOT EXISTS teaching_audio_url TEXT,
    ADD COLUMN IF NOT EXISTS teaching_video_url TEXT;

UPDATE sub_module_lessons sml
SET
    title = qs.title,
    description = qs.description
FROM question_sets qs
WHERE sml.question_set_id IS NOT NULL
  AND qs.id = sml.question_set_id;

UPDATE sub_module_lessons
SET title = 'Lesson'
WHERE title IS NULL OR trim(title) = '';

UPDATE sub_module_lessons
SET teaching_video_url = intro_video_url
WHERE intro_video_url IS NOT NULL;

ALTER TABLE sub_module_lessons DROP CONSTRAINT IF EXISTS sub_module_lessons_question_set_id_fkey;
ALTER TABLE sub_module_lessons DROP CONSTRAINT IF EXISTS sub_module_lessons_question_set_id_key;

ALTER TABLE sub_module_lessons DROP COLUMN IF EXISTS question_set_id;
ALTER TABLE sub_module_lessons DROP COLUMN IF EXISTS intro_video_url;

ALTER TABLE sub_module_lessons
    ALTER COLUMN title SET NOT NULL,
    ALTER COLUMN title SET DEFAULT 'Lesson';
