import os
import argparse
from dotenv import load_dotenv
from mcp_memory_builder import logger

load_dotenv()


def get_config():
    parser = argparse.ArgumentParser(description="MCP Memory Search Configuration")

    # Neo4j configuration arguments
    parser.add_argument(
        "--neo4j-uri",
        default=os.getenv("NEO4J_URI"),
        help="Neo4j URI. Will use environment variable NEO4J_URI if not provided.",
    )
    parser.add_argument(
        "--neo4j-user",
        default=os.getenv("NEO4J_USER"),
        help="Neo4j user. Will use environment variable NEO4J_USER if not provided.",
    )
    parser.add_argument(
        "--neo4j-password",
        default=os.getenv("NEO4J_PASSWORD"),
        help="Neo4j password. Will use environment variable NEO4J_PASSWORD if not provided.",
    )

    # OpenAI configuration arguments
    parser.add_argument(
        "--openai-api-key",
        default=os.getenv("OPENAI_API_KEY"),
        help="OpenAI API key. Will use environment variable OPENAI_API_KEY if not provided.",
    )

    # Model configuration arguments
    parser.add_argument(
        "--model-name",
        default=os.getenv("MODEL_NAME", "gpt-4o"),
        help="Model name. Will use environment variable MODEL_NAME if not provided.",
    )

    
    args = parser.parse_args()

    # === Neo4j Configuration ===
    neo4j_uri = args.neo4j_uri
    neo4j_user = args.neo4j_user
    neo4j_password = args.neo4j_password

    # === OpenAI Configuration ===
    openai_api_key = args.openai_api_key

    # === Model Configuration ===
    model_name = args.model_name



    logger.info(f"Final NEO4J_URI: {neo4j_uri}")
    logger.info(f"Final NEO4J_USER: {'Set' if neo4j_user else 'Not set'}")
    logger.info(f"Final NEO4J_PASSWORD: {'Set' if neo4j_password else 'Not set'}")
    logger.info(f"Final OPENAI_API_KEY: {'Set' if openai_api_key else 'Not set'}")
    logger.info(f"Final MODEL_NAME: {model_name}")

    return neo4j_uri, neo4j_user, neo4j_password, openai_api_key, model_name

# Get configuration values for all services
NEO4J_URI, NEO4J_USER, NEO4J_PASSWORD, OPENAI_API_KEY, MODEL_NAME = get_config()


# Zep optional validation
if not NEO4J_URI:
    logger.error("Neo4j URI not provided. Cannot connect to Neo4j.")
if not NEO4J_USER:
    logger.warning("Neo4j user not provided.")
if not NEO4J_PASSWORD:
    logger.warning("Neo4j password not provided.")
if not OPENAI_API_KEY:
    logger.error("OpenAI API key not provided. Cannot use OpenAI services.")


# Export values for use in other modules
__all__ = [
    "NEO4J_URI",
    "NEO4J_USER",
    "NEO4J_PASSWORD",
    "OPENAI_API_KEY",
    "MODEL_NAME"
]

