-- Capstone assessments: sub-module scoped, backed by question_sets (type CAPSTONE).

ALTER TABLE question_sets DROP CONSTRAINT IF EXISTS question_sets_set_type_check;
ALTER TABLE question_sets ADD CONSTRAINT question_sets_set_type_check
    CHECK (set_type IN (
        'PRACTICE',
        'INITIAL_ASSESSMENT',
        'QUIZ',
        'EXAM',
        'SURVEY',
        'CAPSTONE'
    ));

CREATE TABLE IF NOT EXISTS sub_module_capstones (
    id BIGSERIAL PRIMARY KEY,
    sub_module_id BIGINT NOT NULL REFERENCES sub_modules(id) ON DELETE CASCADE,
    title VARCHAR(255) NOT NULL,
    description TEXT,
    tips TEXT,
    thumbnail TEXT,
    question_set_id BIGINT NOT NULL REFERENCES question_sets(id) ON DELETE CASCADE,
    display_order INT NOT NULL DEFAULT 0,
    is_active BOOLEAN NOT NULL DEFAULT TRUE,
    created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
    UNIQUE (question_set_id)
);

CREATE INDEX IF NOT EXISTS idx_sub_module_capstones_sub_module_id
    ON sub_module_capstones (sub_module_id);
