"""Conversation model."""

from sqlalchemy.orm import Mapped, mapped_column
from sqlalchemy import String, Integer, Text, Boolean, DateTime
from datetime import datetime

from ..db.base import BaseModel

class Conversation(BaseModel):
    """Conversation model."""
    
    __tablename__ = "conversations"
    
    title: Mapped[str] = mapped_column(String(200), nullable=False)
    user_id: Mapped[int] = mapped_column(Integer, nullable=False)  # Removed ForeignKey("users.id")
    knowledge_base_id: Mapped[int | None] = mapped_column(Integer, nullable=True)  # Removed ForeignKey("knowledge_bases.id")
    system_prompt: Mapped[str | None] = mapped_column(Text, nullable=True)
    model_name: Mapped[str] = mapped_column(String(100), nullable=False, default="gpt-3.5-turbo")
    temperature: Mapped[str] = mapped_column(String(10), nullable=False, default="0.7")
    max_tokens: Mapped[int] = mapped_column(Integer, nullable=False, default=2048)
    is_archived: Mapped[bool] = mapped_column(Boolean, default=False, nullable=False)
    message_count: Mapped[int] = mapped_column(Integer, default=0, nullable=False)
    last_message_at: Mapped[datetime | None] = mapped_column(nullable=True)
    
    # Relationships removed to eliminate foreign key constraints
    
    def __repr__(self):
        return f"<Conversation(id={self.id}, title='{self.title}', user_id={self.user_id})>"    

    @property
    def message_count(self):
        """Get the number of messages in this conversation."""
        return len(self.messages)
    
    @property
    def last_message_at(self):
        """Get the timestamp of the last message."""
        return self.messages[-1].created_at or self.created_at
