"""Configuration for per-thread memory mechanism."""

from pydantic import BaseModel, Field


class ThreadMemorySqliteConfig(BaseModel):
    path: str = Field(default="thread_memory.db", description="SQLite database file path")


class ThreadMemoryMysqlConfig(BaseModel):
    host: str = Field(default="localhost")
    port: int = Field(default=3306)
    user: str = Field(default="root")
    password: str = Field(default="")
    database: str = Field(default="deerflow")


class ThreadMemoryDatabaseConfig(BaseModel):
    type: str = Field(default="sqlite", description="Database type: sqlite or mysql")
    sqlite: ThreadMemorySqliteConfig = Field(default_factory=ThreadMemorySqliteConfig)
    mysql: ThreadMemoryMysqlConfig = Field(default_factory=ThreadMemoryMysqlConfig)


class ThreadMemoryConfig(BaseModel):
    enabled: bool = Field(default=True)
    debounce_seconds: int = Field(default=30, ge=1, le=300)
    model_name: str | None = Field(default=None)
    max_facts: int = Field(default=100, ge=10, le=500)
    fact_confidence_threshold: float = Field(default=0.7, ge=0.0, le=1.0)
    injection_enabled: bool = Field(default=True)
    max_injection_tokens: int = Field(default=2000, ge=100, le=8000)
    bootstrap_from_global: bool = Field(default=False)
    database: ThreadMemoryDatabaseConfig = Field(default_factory=ThreadMemoryDatabaseConfig)


_thread_memory_config: ThreadMemoryConfig = ThreadMemoryConfig()


def get_thread_memory_config() -> ThreadMemoryConfig:
    return _thread_memory_config


def set_thread_memory_config(config: ThreadMemoryConfig) -> None:
    global _thread_memory_config
    _thread_memory_config = config


def load_thread_memory_config_from_dict(config_dict: dict) -> None:
    global _thread_memory_config
    _thread_memory_config = ThreadMemoryConfig(**config_dict)
