import logging
import os
import platform
from pathlib import Path

from dotenv import load_dotenv
from dynaconf import Dynaconf
from platformdirs import user_documents_dir

# Setup logging
logger = logging.getLogger(__name__)


def get_config_dir():

    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"
SEARCH_ENGINES_FILE = CONFIG_DIR / "search_engines.toml"

LOCAL_COLLECTIONS_FILE = CONFIG_DIR / "local_collections.toml"

# Define data directory for database
DATA_DIR = os.path.abspath(os.path.join(os.path.dirname(__file__), "..", "..", "data"))
os.makedirs(DATA_DIR, exist_ok=True)
DB_PATH = os.path.join(DATA_DIR, "ldr.db")

env_file = CONFIG_DIR / ".env"

if env_file.exists():
    logger.info(f"Loading environment variables from: {env_file}")
    load_dotenv(dotenv_path=env_file)
else:
    logger.warning(
        f"Warning: .env file not found at {env_file}. Trying secondary location."
    )
    env_file_secondary = get_config_dir() / ".env"
    if env_file_secondary.exists():
        get_config_dir() / "config"
        logger.info(f"Loading environment variables from: {env_file_secondary}")
        load_dotenv(dotenv_path=env_file_secondary)
    else:
        logger.warning(f"Warning: .env file also not found at {env_file_secondary}.")


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


def init_config_files():
    """Initialize config files if they don't exist"""
    import os
    import platform
    import shutil

    # 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}")
                # Add note about database-first settings
                with open(settings_file, "a") as f:
                    f.write(
                        "\n\n# NOTE: Settings in this file are used as fallback only.\n"
                    )
                    f.write(
                        "# Settings stored in the database (ldr.db) take precedence.\n"
                    )
                    f.write(
                        "# To modify settings permanently, use the web interface settings page.\n"
                    )

            # 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}")
                # Add note about database-first settings
                with open(search_engines_file, "a") as f:
                    f.write(
                        "\n\n# NOTE: Settings in this file are used as fallback only.\n"
                    )
                    f.write(
                        "# Settings stored in the database (ldr.db) take precedence.\n"
                    )
                    f.write(
                        "# To modify search settings permanently, use the web interface settings page.\n"
                    )

                # 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}")
            # Add note about database-first settings
            with open(settings_file, "a") as f:
                f.write(
                    "\n\n# NOTE: Settings in this file are used as fallback only.\n"
                )
                f.write("# Settings stored in the database (ldr.db) take precedence.\n")
                f.write(
                    "# To modify settings permanently, use the web interface settings page.\n"
                )

        # 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}")
            # Add note about database-first settings
            with open(search_engines_file, "a") as f:
                f.write(
                    "\n\n# NOTE: Settings in this file are used as fallback only.\n"
                )
                f.write("# Settings stored in the database (ldr.db) take precedence.\n")
                f.write(
                    "# To modify search settings permanently, use the web interface settings page.\n"
                )

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


# Add a comment to explain the DB-first settings approach
logger.info(
    "Using database-first settings approach. TOML files are used as fallback only."
)

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

# Initialize config files on import
init_config_files()
