-- Revert unified questions migration
-- This will recreate the old tables but data migration back is not supported

-- Recreate assessment tables
CREATE TABLE IF NOT EXISTS assessment_questions (
    id BIGSERIAL PRIMARY KEY,
    title TEXT NOT NULL,
    description TEXT,
    question_type VARCHAR(30) NOT NULL CHECK (question_type IN ('MULTIPLE_CHOICE', 'SHORT_ANSWER', 'TRUE_FALSE')),
    difficulty_level TEXT,
    points INT NOT NULL DEFAULT 1,
    is_active BOOLEAN NOT NULL DEFAULT TRUE,
    created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMPTZ
);

CREATE TABLE IF NOT EXISTS assessment_question_options (
    id BIGSERIAL PRIMARY KEY,
    question_id BIGINT NOT NULL REFERENCES assessment_questions(id) ON DELETE CASCADE,
    option_text TEXT NOT NULL,
    option_order INT NOT NULL DEFAULT 0,
    is_correct BOOLEAN NOT NULL DEFAULT FALSE,
    created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP
);

CREATE TABLE IF NOT EXISTS assessment_short_answers (
    id BIGSERIAL PRIMARY KEY,
    question_id BIGINT NOT NULL REFERENCES assessment_questions(id) ON DELETE CASCADE,
    correct_answer TEXT NOT NULL,
    created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP
);

CREATE TABLE IF NOT EXISTS assessment_attempts (
    id BIGSERIAL PRIMARY KEY,
    user_id BIGINT NOT NULL,
    total_questions INT NOT NULL,
    total_points INT NOT NULL,
    score INT,
    percentage NUMERIC(5,2),
    status VARCHAR(20) NOT NULL DEFAULT 'IN_PROGRESS',
    started_at TIMESTAMPTZ,
    submitted_at TIMESTAMPTZ,
    evaluated_at TIMESTAMPTZ,
    created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMPTZ
);

CREATE TABLE IF NOT EXISTS assessment_attempt_questions (
    id BIGSERIAL PRIMARY KEY,
    attempt_id BIGINT NOT NULL REFERENCES assessment_attempts(id) ON DELETE CASCADE,
    question_id BIGINT NOT NULL REFERENCES assessment_questions(id),
    question_type VARCHAR(30) NOT NULL,
    points INT NOT NULL,
    created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP
);

CREATE TABLE IF NOT EXISTS assessment_attempt_answers (
    id BIGSERIAL PRIMARY KEY,
    attempt_id BIGINT NOT NULL REFERENCES assessment_attempts(id) ON DELETE CASCADE,
    question_id BIGINT NOT NULL REFERENCES assessment_questions(id),
    selected_option_id BIGINT,
    submitted_text TEXT,
    is_correct BOOLEAN,
    awarded_points INT NOT NULL DEFAULT 0,
    created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP
);

-- Recreate practices tables
CREATE TABLE IF NOT EXISTS practices (
    id BIGSERIAL PRIMARY KEY,
    sub_course_id BIGINT,
    title VARCHAR(255) NOT NULL,
    description TEXT,
    banner_image TEXT,
    persona VARCHAR(100),
    status VARCHAR(20) NOT NULL DEFAULT 'DRAFT'
);

CREATE TABLE IF NOT EXISTS practice_questions (
    id BIGSERIAL PRIMARY KEY,
    practice_id BIGINT NOT NULL REFERENCES practices(id) ON DELETE CASCADE,
    question TEXT NOT NULL,
    question_voice_prompt TEXT,
    sample_answer_voice_prompt TEXT,
    sample_answer TEXT,
    tips TEXT,
    type VARCHAR(50) NOT NULL
);

-- Drop new unified tables
DROP TABLE IF EXISTS question_set_items CASCADE;
DROP TABLE IF EXISTS question_sets CASCADE;
DROP TABLE IF EXISTS question_short_answers CASCADE;
DROP TABLE IF EXISTS question_options CASCADE;
DROP TABLE IF EXISTS questions CASCADE;
