from pydantic import BaseModel, Field, ConfigDict # <-- Add ConfigDict here
from typing import List, Literal, Optional
from datetime import datetime

# --- Chat Schemas ---
class ChatRequest(BaseModel):
    """Defines the shape of a request to the /chat endpoint."""
    prompt: str = Field(..., min_length=1)
    # The 'model' can now be specified in the request body to switch models mid-conversation.
    model: Literal["deepseek", "gemini"] = Field("deepseek")
    # Add a new optional boolean field to control the retriever
    load_faiss_retriever: Optional[bool] = Field(False, description="Whether to use the FAISS DB retriever for the chat.")


class ChatResponse(BaseModel):
    """Defines the shape of a successful response from the /chat endpoint."""
    answer: str
    model_used: str

# --- Document Schemas ---
class DocumentCreate(BaseModel):
    title: str
    text: str
    source_url: Optional[str] = None
    author: Optional[str] = None
    user_id: str = "default_user"

class DocumentResponse(BaseModel):
    message: str

class DocumentInfo(BaseModel):
    id: int
    title: str
    source_url: Optional[str] = None
    status: str
    created_at: datetime
    model_config = ConfigDict(from_attributes=True)

class DocumentListResponse(BaseModel):
    documents: List[DocumentInfo]

class DocumentDeleteResponse(BaseModel):
    message: str
    document_id: int

# --- Session Schemas ---
class SessionCreate(BaseModel):
    """Defines the shape for starting a new conversation session."""
    user_id: str
    model: Literal["deepseek", "gemini"] = "deepseek"

class Session(BaseModel):
    """Defines the shape of a session object returned by the API."""
    id: int
    user_id: str
    title: str
    model_name: str
    created_at: datetime
    model_config = ConfigDict(from_attributes=True)

class Message(BaseModel):
    """Defines the shape of a single message within a session's history."""
    # The sender can only be one of two roles.
    sender: Literal["user", "assistant"]
    # The text content of the message.
    content: str
    # The timestamp for when the message was created.
    created_at: datetime

    # Enables creating this schema from a SQLAlchemy database object.
    model_config = ConfigDict(from_attributes=True)

class MessageHistoryResponse(BaseModel):
    """Defines the response for retrieving a session's chat history."""
    session_id: int
    messages: List[Message]