import os
from enum import Enum

from pydantic import Field
from pydantic_settings import BaseSettings


class NodeRole(str, Enum):
    STANDALONE = "standalone"
    MASTER = "master"
    SLAVE = "slave"


class Settings(BaseSettings):
    """配置"""

    # 节点信息
    NODE_VERSION: str = Field(default="", description="节点版本")
    PLATFORM_TAG: str = Field(default="", description="平台标签")
    NODE_ROLE: NodeRole = Field(default=NodeRole.STANDALONE, description="节点角色")

    # 端点连接配置
    ENDPOINT_URL: str = Field(description="计算端点连接地址")
    ENDPOINT_ACCESS_ID: str = Field(description="计算端点访问ID")
    ENDPOINT_ACCESS_TOKEN: str = Field(description="计算端点访问令牌")
    PING_INTERVAL: int = Field(default=5, description="心跳间隔时间(秒)")

    # 计算配置
    CPU_RESOURCE: int = Field(
        default_factory=lambda: os.cpu_count() or 1, description="CPU资源"
    )
    POLARS_MAX_THREADS: int = Field(default=1, description="Polars计算最大线程")

    # 数据同步配置
    DATA_SYNC_LOCAL_DATA_DIR: str = Field(
        default="./data", description="数据同步本地数据目录"
    )

    # 统一 S3 配置（基础数据 + 行情共享）
    S3_ENDPOINT_URL: str = Field(default="", description="S3 Endpoint URL")
    S3_ACCESS_KEY: str = Field(default="", description="S3 Access Key")
    S3_SECRET_KEY: str = Field(default="", description="S3 Secret Key")
    S3_BUCKET: str = Field(default="", description="S3 Bucket")

    # 结果数据COS配置
    RESULT_COS_REGION: str = Field(default="ap-shanghai", description="COS区域")
    RESULT_COS_SECRET_ID: str = Field(default="", description="COS Secret ID")
    RESULT_COS_SECRET_KEY: str = Field(default="", description="COS Secret Key")
    RESULT_COS_BUCKET: str = Field(default="", description="COS Bucket")

    # Clickhouse配置
    CLICKHOUSE_URI: str = Field(default="", description="Clickhouse连接URI")

    # === 废弃配置（向后兼容读取，优先使用新配置） ===
    DATA_SYNC_COS_REGION: str = Field(default="ap-shanghai", description="[废弃] COS区域")
    DATA_SYNC_COS_SECRET_ID: str = Field(default="", description="[废弃] COS Secret ID")
    DATA_SYNC_COS_SECRET_KEY: str = Field(default="", description="[废弃] COS Secret Key")
    DATA_SYNC_COS_BUCKET: str = Field(default="", description="[废弃] COS Bucket")
    DATA_S3_ENDPOINT_URL: str = Field(default="", description="[废弃] 使用 S3_ENDPOINT_URL")
    DATA_S3_KEY_ID: str = Field(default="", description="[废弃] 使用 S3_ACCESS_KEY")
    DATA_S3_SECRET_KEY: str = Field(default="", description="[废弃] 使用 S3_SECRET_KEY")
    DATA_S3_BUCKET: str = Field(default="", description="[废弃] 使用 S3_BUCKET")
    CB_QUOTATION_S3_URI: str = Field(default="", description="[废弃] 使用 NODE_ROLE=slave")
    STK_QUOTATION_S3_URI: str = Field(default="", description="[废弃] 使用 NODE_ROLE=slave")
    ETF_QUOTATION_S3_URI: str = Field(default="", description="[废弃] 使用 NODE_ROLE=slave")
    QUOTATION_S3_KEY_ID: str = Field(default="", description="[废弃] 使用 S3_ACCESS_KEY")
    QUOTATION_S3_SECRET_KEY: str = Field(default="", description="[废弃] 使用 S3_SECRET_KEY")
    QUOTATION_S3_ENDPOINT_URL: str = Field(default="", description="[废弃] 使用 S3_ENDPOINT_URL")

    def get_s3_endpoint(self) -> str:
        """获取 S3 endpoint，优先新配置，兼容旧配置"""
        return self.S3_ENDPOINT_URL or self.DATA_S3_ENDPOINT_URL

    def get_s3_access_key(self) -> str:
        return self.S3_ACCESS_KEY or self.DATA_S3_KEY_ID

    def get_s3_secret_key(self) -> str:
        return self.S3_SECRET_KEY or self.DATA_S3_SECRET_KEY

    def get_s3_bucket(self) -> str:
        return self.S3_BUCKET or self.DATA_S3_BUCKET

    class Config:
        """Pydantic config for settings."""

        env_file = ".env"
        env_file_encoding = "utf-8"
        case_sensitive = True
        extra = "allow"
