-- 301_eid_study_weakness_table.sql — 301_eid_study_weakness.sql 분리본 (single-statement). asyncpg 러너가 prepared statement 로 처리해 multi-statement 거부 → 1 stmt/파일. 내용 불변.

CREATE TABLE IF NOT EXISTS eid_study_weakness (
    id                   BIGSERIAL   PRIMARY KEY,
    user_id              BIGINT      NOT NULL REFERENCES users(id) ON DELETE CASCADE,
    weaknesses           JSONB       NOT NULL,   -- [{topic_id,topic,chronic,relapsed,leech,coverage_gap,unsure,overdue,trend,tier}]
    habit_signals        JSONB       NOT NULL,   -- {avoidance_topics,session_abandon_rate,stale_due_count,skew_topics}
    trend_label          VARCHAR(20) NOT NULL,   -- overall 개선|정체|악화 (코드 산출)
    sample_attempts      INTEGER     NOT NULL DEFAULT 0,
    is_shallow_sample    BOOLEAN     NOT NULL DEFAULT false,
    status               VARCHAR(20) NOT NULL DEFAULT 'active',  -- 'active' | 'disputed'(dispute marker)
    supersedes_id        BIGINT      REFERENCES eid_study_weakness(id) ON DELETE SET NULL,
    actor                VARCHAR(20) NOT NULL,   -- 스탬프(no default) = 'eid'
    source_generated_at  TIMESTAMPTZ NOT NULL,   -- 스탬프(no default) = 집계 시점
    created_at           TIMESTAMPTZ NOT NULL DEFAULT now()
);
