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"
    PUBLISH_SCREENSHOT_DIR: Path = BASE_DIR.parent / "private_outputs" / "publish_screenshots"
    
    # 数据库/缓存
    REDIS_URL: str = "redis://localhost:6379/0"
    DEBUG: bool = True

    # Playwright 配置
    WEIXIN_HEADLESS_MODE: str = "headless-new"
    WEIXIN_USER_AGENT: str = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36"
    WEIXIN_LOCALE: str = "zh-CN"
    WEIXIN_TIMEZONE_ID: str = "Asia/Shanghai"
    WEIXIN_CHROME_PATH: str = "/usr/bin/google-chrome"
    WEIXIN_BROWSER_CHANNEL: str = ""
    WEIXIN_FORCE_SWIFTSHADER: bool = True
    WEIXIN_TRANSCODE_MODE: str = "reencode"
    WEIXIN_DEBUG_ARTIFACTS: bool = False
    WEIXIN_RECORD_VIDEO: bool = False
    WEIXIN_KEEP_SUCCESS_VIDEO: bool = False
    WEIXIN_RECORD_VIDEO_WIDTH: int = 1280
    WEIXIN_RECORD_VIDEO_HEIGHT: int = 720

    # Douyin Playwright 配置
    DOUYIN_HEADLESS_MODE: str = "headless-new"
    DOUYIN_USER_AGENT: str = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/144.0.0.0 Safari/537.36"
    DOUYIN_LOCALE: str = "zh-CN"
    DOUYIN_TIMEZONE_ID: str = "Asia/Shanghai"
    DOUYIN_CHROME_PATH: str = "/usr/bin/google-chrome"
    DOUYIN_BROWSER_CHANNEL: str = ""
    DOUYIN_FORCE_SWIFTSHADER: bool = True

    # Douyin 调试录屏
    DOUYIN_DEBUG_ARTIFACTS: bool = False
    DOUYIN_RECORD_VIDEO: bool = False
    DOUYIN_KEEP_SUCCESS_VIDEO: bool = False
    DOUYIN_RECORD_VIDEO_WIDTH: int = 1280
    DOUYIN_RECORD_VIDEO_HEIGHT: int = 720

    # Xiaohongshu Playwright 配置
    XIAOHONGSHU_HEADLESS_MODE: str = "headless-new"
    XIAOHONGSHU_USER_AGENT: str = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/144.0.0.0 Safari/537.36"
    XIAOHONGSHU_LOCALE: str = "zh-CN"
    XIAOHONGSHU_TIMEZONE_ID: str = "Asia/Shanghai"
    XIAOHONGSHU_CHROME_PATH: str = "/usr/bin/google-chrome"
    XIAOHONGSHU_BROWSER_CHANNEL: str = ""
    XIAOHONGSHU_FORCE_SWIFTSHADER: bool = True
    XIAOHONGSHU_DEBUG_ARTIFACTS: bool = False
    
    # 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) 加速

    # MuseTalk 配置
    MUSETALK_GPU_ID: int = 0                        # GPU ID (默认使用 GPU0)
    MUSETALK_API_URL: str = "http://localhost:8011"  # 常驻服务地址
    MUSETALK_BATCH_SIZE: int = 8                    # 推理批大小
    MUSETALK_VERSION: str = "v15"                   # 模型版本
    MUSETALK_USE_FLOAT16: bool = True               # 半精度加速

    # 混合唇形同步路由
    LIPSYNC_DURATION_THRESHOLD: float = 120.0       # 秒，>=此值用 MuseTalk

    # 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"
    
    # 支付宝配置
    ALIPAY_APP_ID: str = ""
    ALIPAY_PRIVATE_KEY_PATH: str = ""   # 应用私钥 PEM 文件路径
    ALIPAY_PUBLIC_KEY_PATH: str = ""    # 支付宝公钥 PEM 文件路径
    ALIPAY_NOTIFY_URL: str = ""         # 异步通知回调地址（公网可达）
    ALIPAY_RETURN_URL: str = ""         # 支付成功后同步跳转地址
    ALIPAY_SANDBOX: bool = False        # 是否使用沙箱环境
    PAYMENT_AMOUNT: float = 999.00      # 会员价格（元）
    PAYMENT_EXPIRE_DAYS: int = 365      # 会员有效天数

    # CORS 配置 (逗号分隔的域名列表，* 表示允许所有)
    CORS_ORIGINS: str = "*"
    @property
    def LATENTSYNC_DIR(self) -> Path:
        """LatentSync 目录路径 (动态计算)"""
        return self.BASE_DIR.parent.parent / "models" / "LatentSync"

    @property
    def MUSETALK_DIR(self) -> Path:
        """MuseTalk 目录路径 (动态计算)"""
        return self.BASE_DIR.parent.parent / "models" / "MuseTalk"

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

settings = Settings()
