# #region AssistantModels [C:3] [TYPE Module] [SEMANTICS sqlalchemy, assistant, model, schema, audit, assistant-audit-record]
# @BRIEF SQLAlchemy models for assistant audit trail and confirmation tokens.
# @LAYER Domain
# @RELATION DEPENDS_ON -> MappingModels
# @INVARIANT  Assistant records preserve immutable ids and creation timestamps.
# @SIDE_EFFECT  Defines assistant audit/message/confirmation tables
# @DATA_CONTRACT  AssistantData -> AssistantRecord

from datetime import UTC, datetime

from sqlalchemy import JSON, Column, DateTime, String, Text

from .mapping import Base


# #region AssistantAuditRecord [C:3] [TYPE Class]
# @BRIEF Store audit decisions and outcomes produced by assistant command handling.
# @RELATION INHERITS -> MappingModels
# @PRE  user_id must identify the actor for every record.
# @POST  Audit payload remains available for compliance and debugging.
class AssistantAuditRecord(Base):
    __tablename__ = "assistant_audit"

    id = Column(String, primary_key=True)
    user_id = Column(String, index=True, nullable=False)
    conversation_id = Column(String, index=True, nullable=True)
    decision = Column(String, nullable=True)
    task_id = Column(String, nullable=True)
    message = Column(Text, nullable=True)
    payload = Column(JSON, nullable=True)
    created_at = Column(DateTime, default=lambda: datetime.now(UTC), nullable=False)


# #endregion AssistantAuditRecord


# #region AssistantMessageRecord [C:3] [TYPE Class]
# @BRIEF Persist chat history entries for assistant conversations.
# @RELATION INHERITS -> MappingModels
# @PRE  user_id, conversation_id, role and text must be present.
# @POST  Message row can be queried in chronological order.
class AssistantMessageRecord(Base):
    __tablename__ = "assistant_messages"

    id = Column(String, primary_key=True)
    user_id = Column(String, index=True, nullable=False)
    conversation_id = Column(String, index=True, nullable=False)
    role = Column(String, nullable=False)  # user | assistant
    text = Column(Text, nullable=False)
    state = Column(String, nullable=True)
    task_id = Column(String, nullable=True)
    confirmation_id = Column(String, nullable=True)
    payload = Column(JSON, nullable=True)
    created_at = Column(DateTime, default=lambda: datetime.now(UTC), nullable=False)


# #endregion AssistantMessageRecord


# #region AssistantConfirmationRecord [C:3] [TYPE Class]
# @BRIEF Persist risky operation confirmation tokens with lifecycle state.
# @RELATION INHERITS -> MappingModels
# @PRE  intent/dispatch and expiry timestamp must be provided.
# @POST  State transitions can be tracked and audited.
class AssistantConfirmationRecord(Base):
    __tablename__ = "assistant_confirmations"

    id = Column(String, primary_key=True)
    user_id = Column(String, index=True, nullable=False)
    conversation_id = Column(String, index=True, nullable=False)
    state = Column(String, index=True, nullable=False, default="pending")
    intent = Column(JSON, nullable=False)
    dispatch = Column(JSON, nullable=False)
    expires_at = Column(DateTime, nullable=False)
    created_at = Column(DateTime, default=lambda: datetime.now(UTC), nullable=False)
    consumed_at = Column(DateTime, nullable=True)


# #endregion AssistantConfirmationRecord
# #endregion AssistantModels
