"""Configuration loading for AI Bridge."""

import copy
import os
from pathlib import Path
from typing import Dict, Any, Optional

import yaml

from .registry import VENDORS


DEFAULT_CONFIG = {
    "default_vendor": "gemini",
    "vendors": {}
}

# Environment variable prefixes
ENV_PREFIX = "AIB_"


def load_config(config_path: Optional[str] = None) -> Dict[str, Any]:
    """
    Load configuration from the first available YAML file and environment variables.

    Priority (highest to lowest):
    1. Environment variables (AIB_<VENDOR>_API_KEY, AIB_<VENDOR>_BASE_URL)
    2. First available YAML file in this order:
       - specified config file
       - ~/.aib/config.yaml
       - ./config.yaml
    3. Default values
    
    Args:
        config_path: Optional path to config file.
    
    Returns:
        Configuration dictionary.
    """
    config = copy.deepcopy(DEFAULT_CONFIG)
    
    # Try to load YAML config
    paths_to_try = []
    if config_path:
        explicit_path = Path(config_path)
        if not explicit_path.exists():
            raise FileNotFoundError(f"Specified config file not found: {config_path}")
        paths_to_try.append(explicit_path)
    paths_to_try.append(Path.home() / ".aib" / "config.yaml")
    paths_to_try.append(Path("config.yaml"))

    for path in paths_to_try:
        if path.exists():
            try:
                with open(path, "r", encoding="utf-8") as f:
                    yaml_config = yaml.safe_load(f)
                    if yaml_config:
                        # Deep merge vendors
                        if "vendors" in yaml_config:
                            config["vendors"].update(yaml_config.pop("vendors"))
                        config.update(yaml_config)
                break
            except Exception as e:
                import warnings
                warnings.warn(f"Failed to parse config file {path}: {e}")
    
    # Override with global environment variables
    timeout = os.getenv(f"{ENV_PREFIX}TIMEOUT")
    if timeout:
        try:
            config["timeout"] = float(timeout)
        except ValueError:
            import warnings
            warnings.warn(f"Invalid global timeout value: '{timeout}', ignoring")

    # Override with vendor-specific environment variables
    for vendor in VENDORS:
        vendor_upper = vendor.upper()
        
        api_key = os.getenv(f"{ENV_PREFIX}{vendor_upper}_API_KEY")
        base_url = os.getenv(f"{ENV_PREFIX}{vendor_upper}_BASE_URL")
        model = os.getenv(f"{ENV_PREFIX}{vendor_upper}_MODEL")
        timeout = os.getenv(f"{ENV_PREFIX}{vendor_upper}_TIMEOUT")
        
        if api_key or base_url or model or timeout:
            if vendor not in config["vendors"]:
                config["vendors"][vendor] = {}
            
            if api_key:
                config["vendors"][vendor]["api_key"] = api_key
            if base_url:
                config["vendors"][vendor]["base_url"] = base_url
            if model:
                config["vendors"][vendor]["model"] = model
            if timeout:
                try:
                    config["vendors"][vendor]["timeout"] = float(timeout)
                except ValueError:
                    import warnings
                    warnings.warn(f"Invalid timeout value for vendor {vendor}: '{timeout}', ignoring")
    
    return config


def get_vendor_config(config: Dict[str, Any], vendor: str) -> Dict[str, Any]:
    """
    Get configuration for a specific vendor.
    
    Args:
        config: Full configuration dictionary.
        vendor: Vendor name (kimi, qwen, gemini, glm, openai).
    
    Returns:
        Vendor-specific configuration.
    """
    return config.get("vendors", {}).get(vendor.lower(), {})
