"""
Search schemas for the sim-search API.

This module defines the Pydantic models for search-related operations.
"""

from typing import Dict, List, Optional, Any
from datetime import datetime

from pydantic import BaseModel, Field

from app.schemas.query import StructuredQuery


class SearchResult(BaseModel):
    """Search result schema."""
    
    title: str
    url: str
    snippet: str
    source: str
    score: Optional[float] = None
    authors: Optional[Any] = None  # Can be string or list of strings
    year: Optional[str] = None
    pdf_url: Optional[str] = None
    arxiv_id: Optional[str] = None
    categories: Optional[List[str]] = None
    published_date: Optional[str] = None
    updated_date: Optional[str] = None
    full_text: Optional[str] = None


class SearchExecute(BaseModel):
    """Search execution schema."""
    
    structured_query: StructuredQuery
    search_engines: Optional[List[str]] = None
    num_results: Optional[int] = 10
    timeout: Optional[int] = 30


class SearchResults(BaseModel):
    """Search results schema."""
    
    search_id: str
    query: str
    enhanced_query: Optional[str] = None
    results: Dict[str, List[SearchResult]]
    total_results: int
    execution_time: float
    timestamp: datetime = Field(default_factory=datetime.utcnow)


class SearchHistory(BaseModel):
    """Search history schema."""
    
    id: str
    query: str
    enhanced_query: Optional[str] = None
    query_type: Optional[str] = None
    engines: Optional[str] = None
    results_count: int
    created_at: datetime
    
    class Config:
        """Pydantic config."""
        
        from_attributes = True


class SearchHistoryList(BaseModel):
    """Search history list schema."""
    
    searches: List[SearchHistory]
    total: int
