"""eid_study_weakness ORM — 이드 학습 약점 스냅샷 (append-only). migration 301.

워커(workers/study_weakness.py)가 INSERT, study_diagnosis 표면이 최신 active 행 SELECT.
UPDATE/DELETE 는 DB RULE(DO INSTEAD NOTHING)로 차단 — ORM mutate 시도도 no-op(행 불변).
스탬프 actor·source_generated_at 는 NOT NULL no-default → 워커가 명시 제공(누락 INSERT 거부).
"""

from __future__ import annotations

from datetime import datetime

from sqlalchemy import (
    BigInteger,
    Boolean,
    DateTime,
    ForeignKey,
    Integer,
    String,
    func,
)
from sqlalchemy.dialects.postgresql import JSONB
from sqlalchemy.orm import Mapped, mapped_column

from core.database import Base


class EidStudyWeakness(Base):
    __tablename__ = "eid_study_weakness"

    id: Mapped[int] = mapped_column(BigInteger, primary_key=True)
    user_id: Mapped[int] = mapped_column(
        BigInteger, ForeignKey("users.id", ondelete="CASCADE"), nullable=False
    )
    # [{topic_id, topic, chronic, relapsed, unsure, coverage_gap, overdue, trend, tier}]
    weaknesses: Mapped[list] = mapped_column(JSONB, nullable=False)
    # {avoidance_topics, session_abandon_rate, stale_due_count, skew_topics}
    habit_signals: Mapped[dict] = mapped_column(JSONB, nullable=False)
    trend_label: Mapped[str] = mapped_column(String(20), nullable=False)
    sample_attempts: Mapped[int] = mapped_column(Integer, nullable=False, default=0)
    is_shallow_sample: Mapped[bool] = mapped_column(Boolean, nullable=False, default=False)
    status: Mapped[str] = mapped_column(String(20), nullable=False, default="active")
    supersedes_id: Mapped[int | None] = mapped_column(
        BigInteger, ForeignKey("eid_study_weakness.id", ondelete="SET NULL")
    )
    actor: Mapped[str] = mapped_column(String(20), nullable=False)  # 스탬프(no default)
    source_generated_at: Mapped[datetime] = mapped_column(
        DateTime(timezone=True), nullable=False
    )  # 스탬프(no default)
    created_at: Mapped[datetime] = mapped_column(
        DateTime(timezone=True), nullable=False, server_default=func.now()
    )
