from pydantic import Field, field_validator
from pydantic_settings import BaseSettings, SettingsConfigDict
import json
import os


class Config(BaseSettings):
    model_config = SettingsConfigDict(
        extra="ignore",
        env_prefix="GROUP_HORSE_RACING_",
    )

    # 测试模式配置
    TEST_MODE: bool = False
    TESTERS: set[int] = Field(default_factory=set)
    TEST_GROUPS: set[int] = Field(default_factory=set)
    ALLOWED_GROUPS: set[int] = Field(default_factory=set)

    # 奖励配置
    PARTICIPANT_REWARD: int = 20
    CHAMPION_REWARD: int = 150
    MIN_BET: int = 10
    MIN_ODDS: float = 1.2
    RACE_DISTANCE: int = 100
    RACE_TICK_INTERVAL: int = 5
    RACE_RENDER_AS_IMAGE: bool = True
    RACE_IMAGE_WIDTH: int = 900
    RACE_IMAGE_FONT_SIZE: int = 26
    RACE_IMAGE_PADDING: int = 28
    RACE_IMAGE_LINE_SPACING: float = 1.35

    # 消息撤回配置
    MESSAGE_RECALL: dict[str, int] = Field(
        default_factory=lambda: {
            "race_update": 30,
            "registration": 180,
            "bet_confirm": 180,
            "cancel_confirm": 60,
            "error": 60,
            "race_result": 0,
            "leaderboard": 0,
            "help": 0,
            "odds_display": 0,
        }
    )

    # 数据库配置
    RACE_DB_FILE: str = "data/group_horse_racing/race.db"

    # xapi /bot/race 运行时 API 配置
    RACE_API_HOST: str = os.getenv("DANDING_RACE_API_HOST", "https://api.danding.vip/bot/race")
    BOT_USER: str = os.getenv("DANDING_BOT_USER", "1424473282")
    BOT_TOKEN: str = os.getenv(
        "DANDING_BOT_TOKEN",
        os.getenv("DANDING_API_TOKEN", os.getenv("BOT_TOKEN", "")),
    )

    @field_validator("RACE_API_HOST")
    @classmethod
    def validate_race_api_host(cls, value):
        """规范化 xapi 赛马运行时 API 地址。"""

        if not value:
            raise ValueError("RACE_API_HOST cannot be empty")
        return value.rstrip("/")

    @field_validator("BOT_USER")
    @classmethod
    def validate_bot_user(cls, value):
        """Bot 鉴权用户不能为空。"""

        if not value:
            raise ValueError("BOT_USER cannot be empty")
        return value

    @field_validator("TESTERS", "TEST_GROUPS", "ALLOWED_GROUPS", mode="before")
    @classmethod
    def parse_id_sets(cls, v):
        """Parse ID sets from various formats."""
        if isinstance(v, set):
            return v
        if isinstance(v, str):
            return cls._parse_id_set(v)
        if isinstance(v, (list, tuple)):
            return set(int(x) for x in v)
        return v if isinstance(v, set) else set()

    @staticmethod
    def _parse_id_set(v: str) -> set[int]:
        """Parse ID sets from various formats."""
        try:
            parsed = json.loads(v)
            if isinstance(parsed, list):
                return set(int(x) for x in parsed)
        except (json.JSONDecodeError, ValueError, TypeError) as e:
            import logging
            logging.getLogger(__name__).warning(f"TESTERS 解析失败: {v}, error: {e}")
            pass
        try:
            return set(int(x.strip()) for x in v.split(",") if x.strip())
        except ValueError:
            pass
        return set()
