"""Load game moderation profiles from config/moderation.yaml.

Falls back to hardcoded defaults if the YAML file is missing or malformed.
"""

import logging
from pathlib import Path

from infrastructure.guards.moderation import GameProfile

logger = logging.getLogger(__name__)


def load_profiles(config_path: Path | None = None) -> dict[str, GameProfile]:
    """Load game moderation profiles from YAML config.

    Args:
        config_path: Path to moderation.yaml. Defaults to config/moderation.yaml.

    Returns:
        Dict mapping game_id to GameProfile.
    """
    path = config_path or Path("config/moderation.yaml")

    if not path.exists():
        logger.info("Moderation config not found at %s — using defaults", path)
        return {}

    try:
        import yaml
    except ImportError:
        logger.warning("PyYAML not installed — using default moderation profiles")
        return {}

    try:
        data = yaml.safe_load(path.read_text())
    except Exception as exc:
        logger.error("Failed to parse moderation config: %s", exc)
        return {}

    profiles: dict[str, GameProfile] = {}
    for game_id, profile_data in data.get("profiles", {}).items():
        try:
            profiles[game_id] = GameProfile(
                game_id=game_id,
                display_name=profile_data.get("display_name", game_id),
                vocabulary_whitelist=profile_data.get("vocabulary_whitelist", []),
                context_prompt=profile_data.get("context_prompt", ""),
                threshold=float(profile_data.get("threshold", 0.8)),
                fallbacks=profile_data.get("fallbacks", {}),
            )
        except Exception as exc:
            logger.warning("Invalid profile '%s': %s", game_id, exc)

    logger.info("Loaded %d moderation profiles from %s", len(profiles), path)
    return profiles
