"""
MatchMe Studio - Configuration
"""
import os
from pathlib import Path
from dotenv import load_dotenv

load_dotenv()

# ============================================================
# API Keys
# ============================================================

# Volcengine / Doubao (Official)
VOLC_API_KEY = os.getenv("VOLC_API_KEY", "05aed9c1-f5e6-487b-9273-fe7d6be51957")
VOLC_BASE_URL = os.getenv("VOLC_BASE_URL", "https://ark.cn-beijing.volces.com/api/v3")

# Models (Updated with User-Provided Endpoint IDs)
# LLM: Doubao Pro 1.5 (Using provided brain/vision endpoint)
BRAIN_MODEL_ID = os.getenv("BRAIN_MODEL_ID", "ep-20251203231055-dpsp7") 
# Vision: Doubao Vision Pro 1.5
VISION_MODEL_ID = os.getenv("VISION_MODEL_ID", "ep-20251203232121-xjt2s") 
# Image: Doubao Image (Updated to user provided model)
IMAGE_MODEL_ID = os.getenv("IMAGE_MODEL_ID", "ep-20251203231641-wg9nb")
# Video: Doubao Video (PixelDance)
VIDEO_MODEL_ID = os.getenv("VIDEO_MODEL_ID", "ep-20251207100506-rjx4x")

# Doubao Specifics (User Provided)
DOUBAO_SCRIPT_MODEL = "ep-20251203231055-dpsp7"
DOUBAO_IMG_MODEL = "ep-20251203231641-wg9nb"


# Text/Brain API (Legacy)
SHUBIAOBIAO_KEY = os.getenv("SHUBIAOBIAO_KEY", "sk-aL167A8sQEyvs40yBfC140Fc0fDa4c198f029aAcF0429108")
SHUBIAOBIAO_BASE_URL = os.getenv("SHUBIAOBIAO_BASE_URL", "https://api.shubiaobiao.cn/v1")
SHUBIAOBIAO_MODEL_TEXT = "gemini-3-pro-preview"

# Image Generation API (Updated)
# Host: https://api.wuyinkeji.com/
# Model: nanoBanana-pro (Gemini)
GEMINI_IMG_KEY = os.getenv("GEMINI_IMG_KEY", "G9rXx3Ag2Xfa7Gs8zou6t6HqeZ")
GEMINI_IMG_API_URL = os.getenv("GEMINI_IMG_API_URL", "https://api.wuyinkeji.com/api/img/nanoBanana-pro")
GEMINI_IMG_DETAIL_URL = os.getenv("GEMINI_IMG_DETAIL_URL", "https://api.wuyinkeji.com/api/img/drawDetail")

# Legacy Image API
SHUBIAOBIAO_IMG_KEY = os.getenv("SHUBIAOBIAO_IMG_KEY", "sk-1yr2h4sJybHB7DED57CeF446D08c4bC989F621Db5b48E70d")
SHUBIAOBIAO_IMG_BASE_URL = os.getenv("SHUBIAOBIAO_IMG_BASE_URL", "https://api2img.shubiaobiao.com")
SHUBIAOBIAO_IMG_MODEL_NAME = "gemini-3-pro-image-preview" 

# Backup
FAL_KEY = os.getenv("FAL_KEY", "")
KLING_ACCESS_KEY = os.getenv("KLING_ACCESS_KEY", "")
KLING_SECRET_KEY = os.getenv("KLING_SECRET_KEY", "")

XI_KEY = os.getenv("XI_KEY", "")

# ============================================================
# Cloudflare R2 Storage
# ============================================================
R2_ENDPOINT = os.getenv("R2_ENDPOINT", "")
R2_ACCESS_KEY = os.getenv("R2_ACCESS_KEY", "")
R2_SECRET_KEY = os.getenv("R2_SECRET_KEY", "")
R2_BUCKET_NAME = os.getenv("R2_BUCKET_NAME", "mms-assets")
# Public URL for accessing uploaded files
R2_PUBLIC_URL = os.getenv("R2_PUBLIC_URL", "https://pub-7942a75aa66d4315a628ee464267ebf4.r2.dev")

# ============================================================
# ElevenLabs Settings (Legacy - for English)
# ============================================================
ELEVENLABS_VOICE_ID = os.getenv("XI_VOICE_ID", "21m00Tcm4TlvDq8ikWAM") 
ELEVENLABS_MODEL = "eleven_turbo_v2_5"

# ============================================================
# Volcengine TTS Settings (火山引擎语音合成 - 中文)
# ============================================================
# 申请地址: https://console.volcengine.com/speech/service/8
VOLC_TTS_APPID = os.getenv("VOLC_TTS_APPID", "6771884088")
VOLC_TTS_ACCESS_TOKEN = os.getenv("VOLC_TTS_ACCESS_TOKEN", "Q5sR2SNfxO8Vb9g2ucsaqfUGOpcpZi3S")
VOLC_TTS_SECRET_KEY = os.getenv("VOLC_TTS_SECRET_KEY", "RXc2WiA6OK6G1xuEZ7cyAU3Q3B5Z1oUx")

# 默认音色
# 抖音热门带货音色推荐：
# - BV700_streaming: 甜美小媛（甜美活泼，适合美妆/好物）- 可能无权限
# - zh_female_santongyongns_saturn_bigtts: 三通永（已验证可用）
# - zh_female_meilinvyou_saturn_bigtts: 美丽女友（已验证可用）
VOLC_TTS_DEFAULT_VOICE = os.getenv("VOLC_TTS_VOICE", "zh_female_santongyongns_saturn_bigtts")

# ============================================================
# Video Settings
# ============================================================
VIDEO_SETTINGS = {
    "width": 1080,
    "height": 1920,
    "fps": 30,
    "format": "mp4",
    "codec": "libx264",
}

# ============================================================
# Paths
# ============================================================
BASE_DIR = Path(__file__).parent
OUTPUT_DIR = BASE_DIR / "output"
TEMP_DIR = BASE_DIR / "temp"
ASSETS_DIR = BASE_DIR / "assets"
FONTS_DIR = ASSETS_DIR / "fonts"

# Ensure directories exist
OUTPUT_DIR.mkdir(exist_ok=True)
TEMP_DIR.mkdir(exist_ok=True)
ASSETS_DIR.mkdir(exist_ok=True)
FONTS_DIR.mkdir(exist_ok=True)

# ============================================================
# Database Configuration
# ============================================================
# Format: postgresql://user:password@host:port/dbname
# Default to SQLite if not provided
DB_CONNECTION_STRING = os.getenv("DB_CONNECTION_STRING", f"sqlite:///{BASE_DIR}/video_flow.db")

# ============================================================
# Font Settings (字体配置)
# ============================================================
# 优先检测系统字体，防止乱码
SYSTEM_FONTS = [
    str(FONTS_DIR / "SmileySans-Oblique.otf"),
    str(FONTS_DIR / "HarmonyOS-Sans-SC-Regular.ttf"),
    str(FONTS_DIR / "HarmonyOS-Sans-SC-Bold.ttf"),
    str(FONTS_DIR / "NotoSansSC-Regular.otf"),
    str(FONTS_DIR / "NotoSansSC-Bold.otf"),
    "/System/Library/Fonts/PingFang.ttc",
    "/System/Library/Fonts/STHeiti Medium.ttc",
    "/System/Library/Fonts/Supplemental/Arial Unicode.ttf",
]

DEFAULT_FONT = str(FONTS_DIR / "NotoSansSC-Regular.otf")
DEFAULT_FONT_BOLD = str(FONTS_DIR / "NotoSansSC-Bold.otf")

# 检查项目字体是否存在，不存在则使用系统字体
def pick_font():
    for f in SYSTEM_FONTS:
        if os.path.exists(f) and os.path.getsize(f) > 1000:
            return f
    return "/System/Library/Fonts/PingFang.ttc"

DEFAULT_FONT = pick_font()
DEFAULT_FONT_BOLD = DEFAULT_FONT

# 花字样式预设
FANCY_TEXT_STYLES = {
    "subtitle": {
        "font_size": 48,
        "font_color": "#FFFFFF",
        "stroke_color": "#000000",
        "stroke_width": 3
    },
    "highlight": {
        "font_size": 56,
        "font_color": "#FFE66D",
        "stroke_color": "#000000",
        "stroke_width": 4
    },
    "warning": {
        "font_size": 52,
        "font_color": "#FF4444",
        "stroke_color": "#FFFFFF",
        "stroke_width": 4
    },
    "price": {
        "font_size": 72,
        "price_color": "#FF4444",
        "stroke_color": "#FFFFFF",
        "stroke_width": 5
    },
    "button": {
        "font_size": 36,
        "font_color": "#FFFFFF",
        "bg_color": "#FF6B35",
        "corner_radius": 25
    }
}
