from sqlalchemy import Column, String, Text, Integer, ForeignKey, Boolean, DateTime, Enum as SQLEnum
from app.models.base import BaseModel, utc_now


class MemorySummary(BaseModel):
    """
    对话摘要 — 中期记忆
    当一段对话超过阈值轮数时，自动生成摘要存入此表
    """
    __tablename__ = "memory_summaries"

    user_id = Column(String(36), ForeignKey("users.id"), nullable=False, index=True)
    conversation_id = Column(String(36), ForeignKey("conversations.id"), nullable=False, index=True)
    summary_text = Column(Text, nullable=False)           # 摘要内容
    turn_count = Column(Integer, default=0)                # 摘要时累计轮数
    summary_at = Column(DateTime, default=utc_now, nullable=False)


class UserMemory(BaseModel):
    """
    用户画像记忆 — 长期记忆
    从对话中提取的用户事实、偏好、目标
    """
    __tablename__ = "user_memories"

    user_id = Column(String(36), ForeignKey("users.id"), nullable=False, index=True)
    memory_type = Column(String(50), nullable=False)       # fact | preference | goal | habit | other
    content = Column(Text, nullable=False)                  # 记忆内容
    importance = Column(Integer, default=5)                # 重要程度 1-10
    is_recalled = Column(Boolean, default=False)           # 是否在当前对话中被召回
    recall_count = Column(Integer, default=0)            # 被召回次数
    source_conversation_id = Column(String(36), nullable=True)  # 来源对话
    extracted_at = Column(DateTime, default=utc_now, nullable=False)
    last_recalled_at = Column(DateTime, nullable=True)
