"""study_quiz_session_analysis ORM (Phase 4-B v1) — 세션 단위 분석 결과 캐시.

session_id PK — 한 세션 = 한 분석 결과. worker 가 ON CONFLICT DO UPDATE 로 UPSERT.
job 이력은 study_quiz_session_jobs 에 별도 누적, 결과 캐시는 1 row.
is_stale=TRUE 는 [재생성] 클릭 후 worker 처리 끝까지만.
"""

from __future__ import annotations

from datetime import datetime

from sqlalchemy import BigInteger, Boolean, DateTime, ForeignKey, String, Text
from sqlalchemy.orm import Mapped, mapped_column

from core.database import Base


class StudyQuizSessionAnalysis(Base):
    __tablename__ = "study_quiz_session_analysis"

    study_quiz_session_id: Mapped[int] = mapped_column(
        BigInteger,
        ForeignKey("study_quiz_sessions.id", ondelete="CASCADE"),
        primary_key=True,
    )
    user_id: Mapped[int] = mapped_column(
        BigInteger, ForeignKey("users.id", ondelete="CASCADE"), nullable=False
    )
    summary_md: Mapped[str] = mapped_column(Text, nullable=False)
    confidence: Mapped[str | None] = mapped_column(String(10))
    model_name: Mapped[str | None] = mapped_column(String(120))
    generated_at: Mapped[datetime] = mapped_column(
        DateTime(timezone=True), default=datetime.now, nullable=False
    )
    is_stale: Mapped[bool] = mapped_column(Boolean, nullable=False, default=False)
