# #region DatasetReviewClarificationModels [C:3] [TYPE Module] [SEMANTICS sqlalchemy, dataset, review, clarification, model]
# @BRIEF Clarification session, question, option, and answer models for guided review flow.
# @LAYER Domain
# @RELATION DEPENDS_ON -> [DatasetReviewEnums]
# @RELATION DEPENDS_ON -> [MappingModels]
# @INVARIANT  Only one active clarification question may exist at a time per session.
# @PRE  Database engine initialized
# @POST  Clarification ORM models registered
# @SIDE_EFFECT  Defines dataset review clarification tables
# @DATA_CONTRACT  ClarificationData -> ClarificationRecord

from datetime import UTC, datetime
import uuid

from sqlalchemy import (
    Boolean,
    Column,
    DateTime,
    Enum as SQLEnum,
    ForeignKey,
    Integer,
    String,
    Text,
)
from sqlalchemy.orm import relationship

from src.models.dataset_review_pkg._enums import (
    AnswerKind,
    ClarificationStatus,
    QuestionState,
)
from src.models.mapping import Base


# #region ClarificationSession [C:2] [TYPE Class]
# @BRIEF One clarification session aggregate owning questions and tracking resolution progress.
# @RELATION DEPENDS_ON -> [DatasetReviewSession]
class ClarificationSession(Base):
    __tablename__ = "clarification_sessions"

    clarification_session_id = Column(String, primary_key=True, default=lambda: str(uuid.uuid4()))
    session_id = Column(String, ForeignKey("dataset_review_sessions.session_id"), nullable=False)
    status = Column(SQLEnum(ClarificationStatus), nullable=False, default=ClarificationStatus.PENDING)
    current_question_id = Column(String, nullable=True)
    resolved_count = Column(Integer, nullable=False, default=0)
    remaining_count = Column(Integer, nullable=False, default=0)
    summary_delta = Column(Text, nullable=True)
    started_at = Column(DateTime, default=lambda: datetime.now(UTC), nullable=False)
    updated_at = Column(DateTime, default=lambda: datetime.now(UTC), onupdate=lambda: datetime.now(UTC), nullable=False)
    completed_at = Column(DateTime, nullable=True)

    session = relationship("DatasetReviewSession", back_populates="clarification_sessions")
    questions = relationship("ClarificationQuestion", back_populates="clarification_session", cascade="all, delete-orphan")


# #endregion ClarificationSession


# #region ClarificationQuestion [C:2] [TYPE Class]
# @BRIEF One clarification question with priority ordering, options, and state machine.
# @RELATION DEPENDS_ON -> [ClarificationSession]
class ClarificationQuestion(Base):
    __tablename__ = "clarification_questions"

    question_id = Column(String, primary_key=True, default=lambda: str(uuid.uuid4()))
    clarification_session_id = Column(String, ForeignKey("clarification_sessions.clarification_session_id"), nullable=False)
    topic_ref = Column(String, nullable=False)
    question_text = Column(Text, nullable=False)
    why_it_matters = Column(Text, nullable=False)
    current_guess = Column(Text, nullable=True)
    priority = Column(Integer, nullable=False, default=0)
    state = Column(SQLEnum(QuestionState), nullable=False, default=QuestionState.OPEN)
    created_at = Column(DateTime, default=lambda: datetime.now(UTC), nullable=False)
    updated_at = Column(DateTime, default=lambda: datetime.now(UTC), onupdate=lambda: datetime.now(UTC), nullable=False)

    clarification_session = relationship("ClarificationSession", back_populates="questions")
    options = relationship("ClarificationOption", back_populates="question", cascade="all, delete-orphan")
    answer = relationship("ClarificationAnswer", back_populates="question", uselist=False, cascade="all, delete-orphan")


# #endregion ClarificationQuestion


# #region ClarificationOption [C:1] [TYPE Class]
# @BRIEF One selectable option for a clarification question with recommendation flag.
# @RELATION DEPENDS_ON -> [ClarificationQuestion]
class ClarificationOption(Base):
    __tablename__ = "clarification_options"

    option_id = Column(String, primary_key=True, default=lambda: str(uuid.uuid4()))
    question_id = Column(String, ForeignKey("clarification_questions.question_id"), nullable=False)
    label = Column(String, nullable=False)
    value = Column(String, nullable=False)
    is_recommended = Column(Boolean, nullable=False, default=False)
    display_order = Column(Integer, nullable=False, default=0)

    question = relationship("ClarificationQuestion", back_populates="options")


# #endregion ClarificationOption


# #region ClarificationAnswer [C:2] [TYPE Class]
# @BRIEF One persisted clarification answer with impact summary and feedback tracking.
# @RELATION DEPENDS_ON -> [ClarificationQuestion]
class ClarificationAnswer(Base):
    __tablename__ = "clarification_answers"

    answer_id = Column(String, primary_key=True, default=lambda: str(uuid.uuid4()))
    question_id = Column(String, ForeignKey("clarification_questions.question_id"), nullable=False, unique=True)
    answer_kind = Column(SQLEnum(AnswerKind), nullable=False)
    answer_value = Column(Text, nullable=True)
    answered_by_user_id = Column(String, nullable=False)
    impact_summary = Column(Text, nullable=True)
    user_feedback = Column(Text, nullable=True)
    created_at = Column(DateTime, default=lambda: datetime.now(UTC), nullable=False)

    question = relationship("ClarificationQuestion", back_populates="answer")


# #endregion ClarificationAnswer


# #endregion DatasetReviewClarificationModels
