-- 206_study_quiz_sessions.sql (1/4)
-- PR-10: 문제풀이 세션 기록 + 이어풀기.
-- 한 토픽의 한 회차 풀이 = 한 세션. question_ids 는 출제 순서 스냅샷.
-- 이어풀기: status='in_progress' + cursor (다음 풀 문제 index, 0-based).
-- 결과 카드: status='done' 후 correct/wrong/unsure_count + finished_at.
--
-- status 권장값: in_progress / done / abandoned (강한 enum 미사용).

CREATE TABLE IF NOT EXISTS study_quiz_sessions (
    id                  BIGSERIAL PRIMARY KEY,
    user_id             BIGINT NOT NULL REFERENCES users(id) ON DELETE CASCADE,
    study_topic_id      BIGINT NOT NULL REFERENCES study_topics(id) ON DELETE CASCADE,
    target_per_subject  INT NOT NULL DEFAULT 20,
    subject_filter      VARCHAR(120),
    wrong_only          BOOLEAN NOT NULL DEFAULT FALSE,
    question_ids        JSONB NOT NULL,
    subject_distribution JSONB NOT NULL DEFAULT '{}'::jsonb,
    status              VARCHAR(20) NOT NULL DEFAULT 'in_progress',
    cursor              INT NOT NULL DEFAULT 0,
    correct_count       INT NOT NULL DEFAULT 0,
    wrong_count         INT NOT NULL DEFAULT 0,
    unsure_count        INT NOT NULL DEFAULT 0,
    finished_at         TIMESTAMPTZ,
    created_at          TIMESTAMPTZ NOT NULL DEFAULT NOW(),
    updated_at          TIMESTAMPTZ NOT NULL DEFAULT NOW()
);
