from pydantic_settings import BaseSettings
from pathlib import Path

class Settings(BaseSettings):
    # 基础路径配置
    BASE_DIR: Path = Path(__file__).resolve().parent.parent
    UPLOAD_DIR: Path = BASE_DIR.parent / "uploads"
    OUTPUT_DIR: Path = BASE_DIR.parent / "outputs"
    ASSETS_DIR: Path = BASE_DIR.parent / "assets"
    
    # 数据库/缓存
    REDIS_URL: str = "redis://localhost:6379/0"
    DEBUG: bool = True
    
    # TTS 配置
    DEFAULT_TTS_VOICE: str = "zh-CN-YunxiNeural"
    MAX_UPLOAD_SIZE_MB: int = 500
    
    # LatentSync 配置
    LATENTSYNC_GPU_ID: int = 1                      # GPU ID (默认使用 GPU1)
    LATENTSYNC_LOCAL: bool = True                   # 使用本地推理 (False 则使用远程 API)
    LATENTSYNC_API_URL: str = "http://localhost:8007"  # 远程 API 地址
    LATENTSYNC_INFERENCE_STEPS: int = 20            # 推理步数 [20-50]
    LATENTSYNC_GUIDANCE_SCALE: float = 1.5          # 引导系数 [1.0-3.0]
    LATENTSYNC_ENABLE_DEEPCACHE: bool = True        # 启用 DeepCache 加速
    LATENTSYNC_SEED: int = 1247                     # 随机种子 (-1 则随机)
    LATENTSYNC_USE_SERVER: bool = True              # 使用常驻服务 (Persistent Server) 加速
    
    # Supabase 配置
    SUPABASE_URL: str = ""
    SUPABASE_PUBLIC_URL: str = ""  # 公网访问地址，用于生成前端可访问的 URL
    SUPABASE_KEY: str = ""
    
    # JWT 配置
    JWT_SECRET_KEY: str = "your-secret-key-change-in-production"
    JWT_ALGORITHM: str = "HS256"
    JWT_EXPIRE_HOURS: int = 24
    
    # 管理员配置
    ADMIN_PHONE: str = ""
    ADMIN_PASSWORD: str = ""
    
    # GLM AI 配置
    GLM_API_KEY: str = ""
    GLM_MODEL: str = "glm-4.7-flash"
    
    @property
    def LATENTSYNC_DIR(self) -> Path:
        """LatentSync 目录路径 (动态计算)"""
        return self.BASE_DIR.parent.parent / "models" / "LatentSync"

    class Config:
        env_file = ".env"
        extra = "ignore"  # 忽略未知的环境变量

settings = Settings()
