import os
import logging
from dotenv import load_dotenv

logger = logging.getLogger(__name__)

def load_env_with_extras():
    """
    Load environment variables from .env file, and optionally from an extra file
    specified by GEMINI_CONFIG_PATH enviroment variable.
    
    The extra file will override values in .env.
    """
    # 1. Load standard .env (DO NOT override existing env vars if they are set in shell)
    # Actually, standard behavior of load_dotenv is not to override.
    load_dotenv()
    
    # 2. Check for GEMINI_CONFIG_PATH
    config_path = os.environ.get("GEMINI_CONFIG_PATH")
    
    if config_path:
        # Resolve path
        if not os.path.isabs(config_path):
            config_path = os.path.abspath(config_path)
            
        if os.path.exists(config_path):
            logger.info(f"Loading extra configuration from: {config_path}")
            # Override=True because we specifically want this file to take precedence
            # over the already loaded .env values (or previous values)
            load_dotenv(dotenv_path=config_path, override=True)
        else:
            logger.warning(f"GEMINI_CONFIG_PATH was set to '{config_path}' but file does not exist.")
