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

from app.models.base import BaseModel, utc_now


class SessionRetrospectiveRecord(BaseModel):
    __tablename__ = "session_retrospectives"

    user_id = Column(String(36), ForeignKey("users.id"), nullable=False, index=True)
    conversation_id = Column(String(36), ForeignKey("conversations.id"), nullable=False, index=True)
    request_message_id = Column(String(36), ForeignKey("messages.id"), nullable=True, index=True)
    response_message_id = Column(String(36), ForeignKey("messages.id"), nullable=True, index=True)
    query_text = Column(Text, nullable=False)
    final_response = Column(Text, nullable=True)
    summary_text = Column(Text, nullable=False)
    task_type = Column(String(64), nullable=True, index=True)
    execution_mode = Column(String(32), nullable=True, index=True)
    primary_agent = Column(String(64), nullable=True)
    verification_status = Column(String(32), nullable=True)
    verification_summary = Column(Text, nullable=True)
    skill_names = Column(JSON, default=list, nullable=False)
    evidence = Column(JSON, default=list, nullable=False)
    task_refs = Column(JSON, default=list, nullable=False)
    payload = Column(JSON, default=dict, nullable=False)
    recorded_at = Column(DateTime, default=utc_now, nullable=False)


class LearningArtifactRecord(BaseModel):
    __tablename__ = "learning_artifacts"

    user_id = Column(String(36), ForeignKey("users.id"), nullable=False, index=True)
    conversation_id = Column(String(36), ForeignKey("conversations.id"), nullable=False, index=True)
    retrospective_id = Column(String(36), ForeignKey("session_retrospectives.id"), nullable=True, index=True)
    artifact_type = Column(String(32), nullable=False, index=True)
    artifact_key = Column(String(128), nullable=True, index=True)
    summary_text = Column(Text, nullable=False)
    payload = Column(JSON, default=dict, nullable=False)
    recorded_at = Column(DateTime, default=utc_now, nullable=False)
