from __future__ import annotations

from functools import lru_cache

from pydantic import Field
from pydantic_settings import BaseSettings, SettingsConfigDict


class Settings(BaseSettings):
    model_config = SettingsConfigDict(env_file=".env", env_file_encoding="utf-8", extra="ignore")

    app_name: str = "AI Knowledge Base V2 Backend"
    app_env: str = "local"
    debug: bool = True
    api_prefix: str = "/api"
    cors_origins: list[str] = Field(
        default_factory=lambda: [
            "http://localhost:5173",
            "http://127.0.0.1:5173",
            "http://localhost:5174",
            "http://127.0.0.1:5174",
            "http://localhost:3000",
            "http://127.0.0.1:3000",
        ]
    )

    database_url: str = "mysql+pymysql://ai_kb:ai_kb@127.0.0.1:3306/ai_knowledge_base_v2?charset=utf8mb4"
    auto_create_tables: bool = False

    jwt_secret_key: str = "change-me-in-production"
    jwt_algorithm: str = "HS256"
    access_token_expire_minutes: int = 60 * 24 * 30

    mock_sms_enabled: bool = True
    mock_sms_code: str = "123456"
    sms_code_expire_minutes: int = 5
    mock_rag_enabled: bool = False
    mock_model_enabled: bool = True
    feishu_mock_enabled: bool = False
    feishu_search_url: str = ""
    feishu_app_id: str = ""
    feishu_app_secret: str = ""
    feishu_timeout_seconds: int = 20
    feishu_retry_count: int = 2

    default_daily_chat_limit: int = 100
    default_user_name_prefix: str = "用户"
    bootstrap_admin_username: str = "admin"
    bootstrap_admin_password: str = "admin123456"
    bootstrap_admin_name: str = "系统管理员"


@lru_cache
def get_settings() -> Settings:
    return Settings()
