# 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():
    import platform
    
    if platform.system() == "Windows":
        # Windows: Use Documents directory
        from platformdirs import user_documents_dir
        config_dir = Path(user_documents_dir()) / "LearningCircuit" / "local-deep-research"
    else:
        # Linux/Mac: Use standard config directory
        from platformdirs import user_config_dir
        config_dir = Path(user_config_dir("local_deep_research", "LearningCircuit"))
    
    logger.info(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"


# 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.search.max_results,
        "region": settings.search.region,
        "time_period": settings.search.time_period,
        "safe_search": settings.search.safe_search,
        "search_snippets_only": settings.search.snippets_only,
        "search_language": settings.search.search_language,
        "max_filtered_results": settings.search.max_filtered_results
    }
    logger.info(f"Search config params: {params}")
    # Create and return search engine
    return factory_get_search(**params)

def init_config_files():
    """Initialize config files if they don't exist"""
    import shutil
    import os
    import sys
    import platform
    
    # Ensure CONFIG_DIR exists with explicit creation
    os.makedirs(CONFIG_DIR, exist_ok=True)
    
    # Get default files path with more reliable approach for Windows
    if platform.system() == "Windows":
        # Use a more reliable method on Windows
        from pkg_resources import resource_filename
        try:
            defaults_dir = Path(resource_filename('local_deep_research', 'defaults'))
            logger.info(f"Using pkg_resources for Windows: {defaults_dir}")
            
            # Create settings.toml if it doesn't exist (with explicit Windows paths)
            settings_file = os.path.join(CONFIG_DIR, "settings.toml")
            default_settings = os.path.join(defaults_dir, "main.toml")
            if not os.path.exists(settings_file) and os.path.exists(default_settings):
                shutil.copyfile(default_settings, settings_file)
                logger.info(f"Created settings.toml at {settings_file}")
            
            # Create llm_config.py if it doesn't exist
            llm_config_file = os.path.join(CONFIG_DIR, "llm_config.py")
            default_llm = os.path.join(defaults_dir, "llm_config.py")
            if not os.path.exists(llm_config_file) and os.path.exists(default_llm):
                shutil.copyfile(default_llm, 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 = os.path.join(CONFIG_DIR, "local_collections.toml")
            default_collections = os.path.join(defaults_dir, "local_collections.toml")
            if not os.path.exists(collections_file) and os.path.exists(default_collections):
                shutil.copyfile(default_collections, collections_file)
                logger.info(f"Created local_collections.toml at {collections_file}")
            
            # Create search_engines.toml if it doesn't exist
            search_engines_file = os.path.join(CONFIG_DIR, "search_engines.toml")
            default_engines = os.path.join(defaults_dir, "search_engines.toml")
            if not os.path.exists(search_engines_file) and os.path.exists(default_engines):
                shutil.copyfile(default_engines, search_engines_file)
                logger.info(f"Created search_engines.toml at {search_engines_file}")

                    # Create .env.template if it doesn't exist
            env_template_file = CONFIG_DIR / ".env.template"
            if not env_template_file.exists():
                shutil.copy(defaults_dir / ".env.template", env_template_file)
                logger.info(f"Created .env.template at {env_template_file}")
                
                # Optionally create an empty .env file if it doesn't exist
                env_file = CONFIG_DIR / ".env"
                if not env_file.exists():
                    with open(env_file, "w") as f:
                        f.write("# Add your environment variables here\n")
                    logger.info(f"Created empty .env file at {env_file}")
        except Exception as e:
            logger.error(f"Error initializing Windows config files: {e}")
    else:
        """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}")
        env_template_file = CONFIG_DIR / ".env.template"
        if not env_template_file.exists():
            shutil.copy(defaults_dir / ".env.template", env_template_file)
            logger.info(f"Created .env.template at {env_template_file}")
            
            # Optionally create an empty .env file if it doesn't exist
            env_file = CONFIG_DIR / ".env"
            if not env_file.exists():
                with open(env_file, "w") as f:
                    f.write("# Add your environment variables here\n")
                logger.info(f"Created empty .env file at {env_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"),
)

