#!/usr/bin/env python3
"""Debug script to test MCP environment and tool registration."""

import os
import sys

print("=== MCP Environment Debug ===")
print(f"Python executable: {sys.executable}")
print(f"Current working directory: {os.getcwd()}")
print()

# Test environment variables
print("=== Environment Variables ===")
chart_api_key = os.getenv("CHART_API_KEY")
print(f"CHART_API_KEY: {'SET' if chart_api_key else 'NOT SET'}")
if chart_api_key:
    print(f"CHART_API_KEY value: {chart_api_key[:8]}..." if len(chart_api_key) > 8 else f"CHART_API_KEY value: {chart_api_key}")

snowflake_user = os.getenv("SNOWFLAKE_USER") 
print(f"SNOWFLAKE_USER: {'SET' if snowflake_user else 'NOT SET'}")

print()

# Test dotenv loading
print("=== Testing dotenv loading ===")
try:
    from dotenv import load_dotenv
    
    # Try loading from current directory
    loaded = load_dotenv()
    print(f"dotenv load_dotenv(): {loaded}")
    
    # Check if .env file exists
    env_file = os.path.join(os.getcwd(), '.env')
    print(f".env file exists at {env_file}: {os.path.exists(env_file)}")
    
    # Try loading specific .env file
    if os.path.exists(env_file):
        loaded_specific = load_dotenv(env_file)
        print(f"dotenv load_dotenv('{env_file}'): {loaded_specific}")
    
    # Re-check environment after dotenv
    chart_api_key_after = os.getenv("CHART_API_KEY")
    print(f"CHART_API_KEY after dotenv: {'SET' if chart_api_key_after else 'NOT SET'}")
    
except ImportError:
    print("python-dotenv not available")

print()

# Test MCP tool registration
print("=== Testing MCP Tool Registration ===")
try:
    from mcp.server.fastmcp import FastMCP
    from point_topic_mcp.tools import register_tools
    
    print("Creating FastMCP instance...")
    mcp = FastMCP(name='debug-test')
    
    print("Registering tools...")
    register_tools(mcp)
    
    print("Checking registered tools...")
    tools = list(mcp._tool_manager._tools.keys())
    print(f"Registered tools: {tools}")
    
    has_auth_tool = any('authenticated' in name.lower() for name in tools)
    print(f"Has authenticated chart tool: {has_auth_tool}")
    
except Exception as e:
    print(f"Error during MCP setup: {e}")
    import traceback
    traceback.print_exc()

print()
print("=== Debug Complete ===")
