"""
Configuration file for search engines.
Loads search engine definitions from the user's configuration.
"""
import logging
import os
import toml
from pathlib import Path
from local_deep_research.config import CONFIG_DIR, LOCAL_COLLECTIONS_FILE

logger = logging.getLogger(__name__)

# Get search engines configuration directly from TOML file
SEARCH_ENGINES = {}
DEFAULT_SEARCH_ENGINE = "wikipedia"  # Default fallback if not specified in config

# Path to the search engines configuration file
SEARCH_ENGINES_FILE = CONFIG_DIR / "search_engines.toml"

# Load directly from TOML file
if os.path.exists(SEARCH_ENGINES_FILE):
    try:
        # Load the TOML file directly
        config_data = toml.load(SEARCH_ENGINES_FILE)
        
        # Extract search engine definitions
        for key, value in config_data.items():
            if key == "DEFAULT_SEARCH_ENGINE":
                DEFAULT_SEARCH_ENGINE = value
            elif isinstance(value, dict):
                SEARCH_ENGINES[key] = value
        
        logger.info(f"Loaded {len(SEARCH_ENGINES)} search engines from configuration file")
    except Exception as e:
        logger.error(f"Error loading search engines from TOML file: {e}")
else:
    logger.warning(f"Search engines configuration file not found: {SEARCH_ENGINES_FILE}")

# Add alias for 'auto' if it exists
if 'auto' in SEARCH_ENGINES and 'meta' not in SEARCH_ENGINES:
    SEARCH_ENGINES['meta'] = SEARCH_ENGINES['auto']

# Register local document collections

if os.path.exists(LOCAL_COLLECTIONS_FILE):
    try:
        local_collections_data = toml.load(LOCAL_COLLECTIONS_FILE)

        for collection, config in local_collections_data.items():
            # Create a new dictionary with required search engine fields
            engine_config = {
                "module_path": "local_deep_research.web_search_engines.engines.search_engine_local",
                "class_name": "LocalSearchEngine",
                "default_params": config,
                "requires_llm": True
            }
            
            # Copy these specific fields to the top level if they exist
            for field in ["strengths", "weaknesses", "reliability", "description"]:
                if field in config:
                    engine_config[field] = config[field]
            
            SEARCH_ENGINES[collection] = engine_config
            
        logger.info(f"Registered local document collections as search engines")
    except Exception as e:
        logger.error(f"Error loading local collections from TOML file: {e}")
# Ensure the meta search engine is still available at the end if it exists
if 'auto' in SEARCH_ENGINES:
    meta_config = SEARCH_ENGINES["auto"]
    SEARCH_ENGINES["auto"] = meta_config