"""
Configuration loading for script pipeline.
"""
import json
import logging
import os
from typing import Dict, Any

from src.constants.common import CHARACTER_ROLES, CHARACTER_GENDERS, SHOT_TYPES, CAMERA_MOVEMENTS, TRANSITIONS

logger = logging.getLogger(__name__)


def load_generation_options() -> Dict[str, Any]:
    """Load generation options from JSON config file with fallback to constants."""
    try:
        # Get config path
        current_dir = os.path.dirname(os.path.abspath(__file__))
        backend_dir = os.path.dirname(os.path.dirname(os.path.dirname(current_dir)))
        config_path = os.path.join(backend_dir, "config", "generation_options.json")

        if os.path.exists(config_path):
            with open(config_path, 'r', encoding='utf-8') as f:
                options = json.load(f)

            # Extract lists from config
            result = {}

            # Character options
            if 'character' in options:
                if 'genders' in options['character']:
                    result['character_genders_zh'] = [opt['value'] for opt in options['character']['genders']['options']]
                    result['character_genders_en'] = [opt.get('en', opt['value']) for opt in options['character']['genders']['options']]
                if 'roles' in options['character']:
                    result['character_roles_zh'] = [opt['value'] for opt in options['character']['roles']['options']]
                    result['character_roles_en'] = [opt.get('en', opt['value']) for opt in options['character']['roles']['options']]
                if 'emotions' in options['character']:
                    result['character_emotions_zh'] = [opt['value'] for opt in options['character']['emotions']['options']]
                    result['character_emotions_en'] = [opt.get('en', opt['value']) for opt in options['character']['emotions']['options']]

            # Scene options
            if 'scene' in options:
                if 'timesOfDay' in options['scene']:
                    result['scene_times_zh'] = [opt['value'] for opt in options['scene']['timesOfDay']['options']]
                    result['scene_times_en'] = [opt.get('en', opt['value']) for opt in options['scene']['timesOfDay']['options']]
                if 'environmentTypes' in options['scene']:
                    result['scene_environments_zh'] = [opt['value'] for opt in options['scene']['environmentTypes']['options']]
                    result['scene_environments_en'] = [opt.get('en', opt['value']) for opt in options['scene']['environmentTypes']['options']]
                if 'weather' in options['scene']:
                    result['scene_weather_zh'] = [opt['value'] for opt in options['scene']['weather']['options']]
                    result['scene_weather_en'] = [opt.get('en', opt['value']) for opt in options['scene']['weather']['options']]

            # Storyboard & Cinematic options
            if 'storyboard' in options:
                if 'shotTypes' in options['storyboard']:
                    result['shot_types_zh'] = [opt['value'] for opt in options['storyboard']['shotTypes']['options']]
                    result['shot_types_en'] = [opt['en'] for opt in options['storyboard']['shotTypes']['options']]
                if 'cameraMovements' in options['storyboard']:
                    result['camera_movements_zh'] = [opt['value'] for opt in options['storyboard']['cameraMovements']['options']]
                    result['camera_movements_en'] = [opt['en'] for opt in options['storyboard']['cameraMovements']['options']]
                if 'transitions' in options['storyboard']:
                    result['transitions_zh'] = [opt['value'] for opt in options['storyboard']['transitions']['options']]
                    result['transitions_en'] = [opt['en'] for opt in options['storyboard']['transitions']['options']]

            if 'cinematic' in options:
                if 'cameraAngles' in options['cinematic']:
                    result['camera_angles_zh'] = [opt['value'] for opt in options['cinematic']['cameraAngles']['options']]
                    result['camera_angles_en'] = [opt.get('en', opt['value']) for opt in options['cinematic']['cameraAngles']['options']]
                if 'lighting' in options['cinematic']:
                    result['lighting_zh'] = [opt['value'] for opt in options['cinematic']['lighting']['options']]
                    result['lighting_en'] = [opt.get('en', opt['value']) for opt in options['cinematic']['lighting']['options']]
                if 'colorStyle' in options['cinematic']:
                    result['color_style_zh'] = [opt['value'] for opt in options['cinematic']['colorStyle']['options']]
                    result['color_style_en'] = [opt.get('en', opt['value']) for opt in options['cinematic']['colorStyle']['options']]
                if 'lenses' in options['cinematic']:
                    result['lenses_zh'] = [opt['value'] for opt in options['cinematic']['lenses']['options']]
                    result['lenses_en'] = [opt.get('en', opt['value']) for opt in options['cinematic']['lenses']['options']]
                if 'focus' in options['cinematic']:
                    result['focus_zh'] = [opt['value'] for opt in options['cinematic']['focus']['options']]
                    result['focus_en'] = [opt.get('en', opt['value']) for opt in options['cinematic']['focus']['options']]

            return result
    except Exception as e:
        logger.warning(f"Failed to load generation options from config: {e}")

    # Fallback to constants
    return {
        'character_genders_zh': CHARACTER_GENDERS.get('zh', []),
        'character_genders_en': CHARACTER_GENDERS.get('en', []),
        'character_roles_zh': CHARACTER_ROLES.get('zh', []),
        'character_roles_en': CHARACTER_ROLES.get('en', []),
        'shot_types_zh': SHOT_TYPES.get('zh', []),
        'shot_types_en': SHOT_TYPES.get('en', []),
        'camera_movements_zh': CAMERA_MOVEMENTS.get('zh', []),
        'camera_movements_en': CAMERA_MOVEMENTS.get('en', []),
        'transitions_zh': TRANSITIONS.get('zh', []),
        'transitions_en': TRANSITIONS.get('en', [])
    }


# Load options once at module level
GENERATION_OPTIONS = load_generation_options()
