"""Shared helpers for project-related services (cinematic defaults, etc.)."""
import json
import logging
import os
from typing import Dict, Optional

logger = logging.getLogger(__name__)
_CINEMATIC_DEFAULTS_CACHE: Optional[Dict[str, str]] = None


def get_cinematic_defaults() -> Dict[str, str]:
    """从 generation_options.json 获取电影字段默认值（带缓存）"""
    global _CINEMATIC_DEFAULTS_CACHE
    if _CINEMATIC_DEFAULTS_CACHE is not None:
        return _CINEMATIC_DEFAULTS_CACHE
    defaults = {
        "camera_angle": "平视",
        "lens": "标准镜头",
        "focus": "自动对焦",
        "lighting": "电影感光效",
        "color_style": "电影感",
    }
    try:
        config_path = os.path.join(
            os.path.dirname(__file__), "..", "config", "generation_options.json"
        )
        with open(config_path, "r", encoding="utf-8") as f:
            options = json.load(f)
            if "cinematic" in options:
                cinematic = options["cinematic"]
                defaults = {
                    "camera_angle": cinematic.get("cameraAngles", {}).get("default", "平视"),
                    "lens": cinematic.get("lenses", {}).get("default", "标准镜头"),
                    "focus": cinematic.get("focus", {}).get("default", "自动对焦"),
                    "lighting": cinematic.get("lighting", {}).get("default", "电影感光效"),
                    "color_style": cinematic.get("colorStyle", {}).get("default", "电影感"),
                }
    except Exception as e:
        logger.warning("Failed to load cinematic defaults from config: %s", e)
    _CINEMATIC_DEFAULTS_CACHE = defaults
    return defaults
