"""
LLM Provider Presets
====================
预设的LLM提供商配置，简化环境变量设置。

使用方式：
  方式1（推荐）：设置 LLM_PROVIDER 环境变量为提供商名称，自动填充 base_url 和 model
    LLM_PROVIDER=deepseek
    LLM_API_KEY=sk-xxx

  方式2：手动指定所有配置（兼容原有方式）
    LLM_API_KEY=sk-xxx
    LLM_BASE_URL=https://api.deepseek.com/v1
    LLM_MODEL_NAME=deepseek-chat

支持的提供商：
  - openai         : OpenAI GPT系列
  - deepseek       : DeepSeek (深度求索)
  - xiaomi_mimo    : Xiaomi MiMo (小米MiMo)
  - alibaba_dashscope : 阿里百炼 (通义千问)
  - minimax        : MiniMax (海螺AI)
"""

from dataclasses import dataclass
from typing import Optional


@dataclass(frozen=True)
class ProviderPreset:
    """LLM提供商预设配置"""
    name: str
    display_name: str
    base_url: str
    default_model: str
    api_key_url: str
    notes: str = ""
    # 某些提供商的响应可能包含<think>标签（如DeepSeek推理模型）
    may_include_think_tags: bool = False


# ============================================================
# Provider Presets
# ============================================================

PROVIDERS: dict[str, ProviderPreset] = {
    "openai": ProviderPreset(
        name="openai",
        display_name="OpenAI",
        base_url="https://api.openai.com/v1",
        default_model="gpt-4o-mini",
        api_key_url="https://platform.openai.com/api-keys",
        notes="GPT-4o-mini recommended for cost efficiency.",
    ),
    "deepseek": ProviderPreset(
        name="deepseek",
        display_name="DeepSeek (深度求索)",
        base_url="https://api.deepseek.com/v1",
        default_model="deepseek-chat",
        api_key_url="https://platform.deepseek.com",
        notes=(
            "deepseek-chat: general purpose; "
            "deepseek-reasoner: reasoning model with <think> tags in output. "
            "Pricing: https://api-docs.deepseek.com/quick_start/pricing"
        ),
        may_include_think_tags=True,
    ),
    "xiaomi_mimo": ProviderPreset(
        name="xiaomi_mimo",
        display_name="Xiaomi MiMo (小米MiMo)",
        base_url="https://api.xiaomimimo.com/v1",
        default_model="mimo-v2.5-pro",
        api_key_url="https://platform.xiaomimimo.com",
        notes=(
            "mimo-v2.5-pro: flagship model; "
            "mimo-v2-flash: fast & economical. "
            "OpenAI SDK compatible. May include <think> tags for reasoning."
        ),
        may_include_think_tags=True,
    ),
    "alibaba_dashscope": ProviderPreset(
        name="alibaba_dashscope",
        display_name="Alibaba DashScope (阿里百炼)",
        base_url="https://dashscope.aliyuncs.com/compatible-mode/v1",
        default_model="qwen-plus",
        api_key_url="https://bailian.console.aliyun.com/",
        notes=(
            "qwen-plus: recommended balance of quality & cost. "
            "High token consumption — try <40 round simulations first."
        ),
    ),
    "minimax": ProviderPreset(
        name="minimax",
        display_name="MiniMax (海螺AI)",
        base_url="https://api.minimax.chat/v1",
        default_model="MiniMax-M2.5",
        api_key_url="https://platform.minimaxi.com/",
        notes="MiniMax-M2.5 may include <think> tags.",
        may_include_think_tags=True,
    ),
}


def get_provider(name: str) -> Optional[ProviderPreset]:
    """
    获取提供商预设配置。

    Args:
        name: 提供商名称（不区分大小写）

    Returns:
        ProviderPreset 或 None（如果未找到）
    """
    return PROVIDERS.get(name.lower().strip())


def list_providers() -> list[dict]:
    """
    列出所有可用的提供商预设。

    Returns:
        提供商信息列表
    """
    return [
        {
            "name": p.name,
            "display_name": p.display_name,
            "base_url": p.base_url,
            "default_model": p.default_model,
            "api_key_url": p.api_key_url,
            "notes": p.notes,
        }
        for p in PROVIDERS.values()
    ]
