"""
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

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
try:
    from local_deep_research.local_collections import register_local_collections
    register_local_collections(SEARCH_ENGINES)
    logger.info(f"Registered local document collections as search engines")
except ImportError:
    logger.info("No local collections configuration found. Local document search is disabled.")

# 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