"""
Configuration settings for the sim-search API.

This module defines the settings for the API, loaded from environment variables.
"""

import os
import secrets
from typing import List, Optional, Dict, Any, Union

from pydantic import AnyHttpUrl, field_validator
from pydantic_settings import BaseSettings


class Settings(BaseSettings):
    """Settings for the sim-search API."""
    
    # API settings
    API_V1_STR: str = "/api/v1"
    PROJECT_NAME: str = "Sim-Search API"
    PROJECT_DESCRIPTION: str = "API for the Sim-Search intelligent research system"
    VERSION: str = "0.1.0"
    
    # Security settings
    SECRET_KEY: str = os.getenv("SECRET_KEY", secrets.token_urlsafe(32))
    ALGORITHM: str = "HS256"
    ACCESS_TOKEN_EXPIRE_MINUTES: int = 60 * 24 * 7  # 7 days
    
    # CORS settings
    CORS_ORIGINS: List[str] = ["*"]
    
    @field_validator("CORS_ORIGINS", mode="before")
    @classmethod
    def assemble_cors_origins(cls, v: Union[str, List[str]]) -> Union[List[str], str]:
        """Parse CORS origins from string or list."""
        if isinstance(v, str) and not v.startswith("["):
            return [i.strip() for i in v.split(",")]
        elif isinstance(v, (list, str)):
            return v
        raise ValueError(v)
    
    # Database settings
    SQLALCHEMY_DATABASE_URI: str = os.getenv(
        "DATABASE_URL", f"sqlite:///./sim-search.db"
    )
    
    # Sim-search settings
    SIM_SEARCH_PATH: str = os.getenv("SIM_SEARCH_PATH", "/Volumes/SAM2/CODE/sim-search")
    
    # Default models for different detail levels
    DEFAULT_MODELS: Dict[str, str] = {
        "brief": "llama-3.1-8b-instant",
        "standard": "llama-3.1-8b-instant",
        "detailed": "llama-3.3-70b-versatile",
        "comprehensive": "llama-3.3-70b-versatile"
    }
    
    model_config = {
        "case_sensitive": True,
        "env_file": ".env",
    }


# Create settings instance
settings = Settings()
