# local_deep_research/config.py
from dynaconf import Dynaconf
from pathlib import Path
import logging
from platformdirs import user_documents_dir
import os
# Setup logging
logger = logging.getLogger(__name__)

# Get config directory
def get_config_dir():
    from platformdirs import user_config_dir
    config_dir = Path(user_config_dir("local_deep_research", "LearningCircuit"))
    print(f"Looking for config in: {config_dir}")
    return config_dir

# Define config paths
CONFIG_DIR = get_config_dir() / "config"
CONFIG_DIR.mkdir(parents=True, exist_ok=True)
SETTINGS_FILE = CONFIG_DIR / "settings.toml"
SECRETS_FILE = CONFIG_DIR / ".secrets.toml"
LLM_CONFIG_FILE = CONFIG_DIR / "llm_config.py"
SEARCH_ENGINES_FILE = CONFIG_DIR / "search_engines.toml"

LOCAL_COLLECTIONS_FILE = CONFIG_DIR / "local_collections.toml"
print("CONFIGDIR:", CONFIG_DIR)
print("SECRETS_FILE:", SECRETS_FILE)
print("SETTINGS_FILE:", SETTINGS_FILE)


# Set environment variable for Dynaconf to use
docs_base = Path(user_documents_dir()) / "local_deep_research"
os.environ["DOCS_DIR"] = str(docs_base)








# Expose get_llm function
def get_llm(*args, **kwargs):
    """
    Helper function to get LLM from llm_config.py
    """
    # Import here to avoid circular imports
    import importlib.util
    import sys
    
    llm_config_path = CONFIG_DIR / "llm_config.py"
    
    # If llm_config.py exists, use it
    if llm_config_path.exists():
        if str(CONFIG_DIR) not in sys.path:
            sys.path.insert(0, str(CONFIG_DIR))
            
        spec = importlib.util.spec_from_file_location("llm_config", llm_config_path)
        llm_config = importlib.util.module_from_spec(spec)
        spec.loader.exec_module(llm_config)
        
        if hasattr(llm_config, "get_llm"):
            return llm_config.get_llm(*args, **kwargs)
    
    # Fallback to utility function
    from .utilties.llm_utils import get_model
    return get_model(*args, **kwargs)

# Expose get_search function
def get_search(search_tool=None):
    """
    Helper function to get search engine
    """
    
    # Use specified tool or default from settings
    tool = search_tool or settings.search.tool
    logger.info(f"Search tool is: {tool}")
    
    # Import here to avoid circular imports
    from .web_search_engines.search_engine_factory import get_search as factory_get_search
    
    # Get search parameters
    params = {
        "search_tool": tool,
        "llm_instance": get_llm(),
        "max_results": settings.get("max_results"),
        "region": settings.get("region"),
        "time_period": settings.get("time_period"),
        "safe_search": settings.get("safe_search"),
        "search_snippets_only": settings.get("snippets_only"),
        "search_language": settings.get("search_language"),
        "max_filtered_results": settings.get("max_filtered_results")
    }
    
    # Create and return search engine
    return factory_get_search(**params)

def init_config_files():
    """Initialize config files if they don't exist"""
    import shutil
    from importlib.resources import files
    
    # Get default files path
    try:
        defaults_dir = files('local_deep_research.defaults')
    except ImportError:
        # Fallback for older Python versions
        from pkg_resources import resource_filename
        defaults_dir = Path(resource_filename('local_deep_research', 'defaults'))
    
    # Create settings.toml if it doesn't exist
    settings_file = CONFIG_DIR / "settings.toml"
    if not settings_file.exists():
        shutil.copy(defaults_dir / "main.toml", settings_file)
        logger.info(f"Created settings.toml at {settings_file}")
    
    # Create llm_config.py if it doesn't exist
    llm_config_file = CONFIG_DIR / "llm_config.py"
    if not llm_config_file.exists():
        shutil.copy(defaults_dir / "llm_config.py", llm_config_file)
        logger.info(f"Created llm_config.py at {llm_config_file}")
        
    # Create local_collections.toml if it doesn't exist
    collections_file = CONFIG_DIR / "local_collections.toml"
    if not collections_file.exists():
        shutil.copy(defaults_dir / "local_collections.toml", collections_file)
        logger.info(f"Created local_collections.toml at {collections_file}")
    
    # Create search_engines.toml if it doesn't exist
    search_engines_file = CONFIG_DIR / "search_engines.toml"
    if not search_engines_file.exists():
        shutil.copy(defaults_dir / "search_engines.toml", search_engines_file)
        logger.info(f"Created search_engines.toml at {search_engines_file}")
        
    secrets_file = CONFIG_DIR / ".secrets.toml"
    if not secrets_file.exists():
        with open(secrets_file, "w") as f:
            f.write("""
# ANTHROPIC_API_KEY = "your-api-key-here"
# OPENAI_API_KEY = "your-openai-key-here"
# GOOGLE_API_KEY = "your-google-key-here"
# SERP_API_KEY = "your-api-key-here"
# GUARDIAN_API_KEY = "your-api-key-here"
# GOOGLE_PSE_API_KEY = "your-google-api-key-here"
# GOOGLE_PSE_ENGINE_ID = "your-programmable-search-engine-id-here"
""")
        
# Initialize config files on import
init_config_files()

# Use an absolute path to your .secrets.toml for testing
secrets_file = Path(SECRETS_FILE)

settings = Dynaconf(
    settings_files=[
        str(SETTINGS_FILE),
        str(LOCAL_COLLECTIONS_FILE),
        str(SEARCH_ENGINES_FILE),  
    ],
    secrets=str(SECRETS_FILE),
    env_prefix="LDR",
    load_dotenv=True,
    envvar_prefix="LDR",
    env_file=str(CONFIG_DIR / ".env"),
)

