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
    log_level: str = "INFO"
    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"
    redis_url: str = ""
    readiness_requires_redis: bool = True
    db_pool_size: int = 10
    db_max_overflow: int = 20
    db_pool_timeout: int = 30
    db_pool_recycle: int = 1800
    auto_create_tables: bool = False

    jwt_secret_key: str = "change-me-in-production"
    config_encryption_key: str = ""
    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
    aliyun_sms_access_key_id: str = ""
    aliyun_sms_access_key_secret: str = ""
    aliyun_sms_sign_name: str = ""
    aliyun_sms_template_code: str = ""
    aliyun_sms_template_param_key: str = "code"
    aliyun_sms_endpoint: str = "dysmsapi.aliyuncs.com"
    aliyun_nls_app_key: str = ""
    aliyun_nls_access_key_id: str = ""
    aliyun_nls_access_key_secret: str = ""
    aliyun_nls_endpoint: str = "https://nls-gateway-cn-shanghai.aliyuncs.com/stream/v1/asr"
    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 = "用户"
    chat_max_active_requests: int = 30
    chat_max_queue_size: int = 90
    chat_queue_timeout_seconds: int = 90
    chat_active_lease_seconds: int = 900
    voice_input_enabled: bool = False
    voice_max_duration_seconds: int = 60
    periodic_report_worker_enabled: bool = True
    periodic_report_poll_seconds: int = 5
    periodic_report_stale_minutes: int = 30
    periodic_report_max_attempts: int = 3
    periodic_report_weekly_enabled: bool = True
    periodic_report_monthly_enabled: bool = True
    periodic_report_timezone: str = "Asia/Shanghai"
    topic_settlement_worker_enabled: bool = True
    topic_settlement_poll_seconds: int = 2
    topic_settlement_stale_minutes: int = 30
    topic_settlement_max_attempts: int = 3
    bootstrap_admin_username: str = ""
    bootstrap_admin_password: str = ""
    bootstrap_admin_name: str = "系统管理员"


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