# [DEF:backend.src.models.assistant:Module]
# @TIER: STANDARD
# @SEMANTICS: assistant, audit, confirmation, chat
# @PURPOSE: SQLAlchemy models for assistant audit trail and confirmation tokens.
# @LAYER: Domain
# @RELATION: DEPENDS_ON -> backend.src.models.mapping
# @INVARIANT: Assistant records preserve immutable ids and creation timestamps.

from datetime import datetime

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

from .mapping import Base


# [DEF:AssistantAuditRecord:Class]
# @TIER: STANDARD
# @PURPOSE: Store audit decisions and outcomes produced by assistant command handling.
# @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=datetime.utcnow, nullable=False)
# [/DEF:AssistantAuditRecord:Class]


# [DEF:AssistantMessageRecord:Class]
# @TIER: STANDARD
# @PURPOSE: Persist chat history entries for assistant conversations.
# @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=datetime.utcnow, nullable=False)
# [/DEF:AssistantMessageRecord:Class]


# [DEF:AssistantConfirmationRecord:Class]
# @TIER: STANDARD
# @PURPOSE: Persist risky operation confirmation tokens with lifecycle state.
# @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=datetime.utcnow, nullable=False)
    consumed_at = Column(DateTime, nullable=True)
# [/DEF:AssistantConfirmationRecord:Class]
# [/DEF:backend.src.models.assistant:Module]
