#!/usr/bin/env python3
"""Minimal MCP server for testing with simplified initialization."""

import logging
import os
from pathlib import Path
import sys
from typing import Optional, Dict, Any

# Add the project directory to the path
sys.path.insert(0, str(Path(__file__).parent))

from mcp.server import FastMCP

logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)


def main():
    """Main entry point."""
    logger.info("Starting Minimal Code Intelligence MCP server...")

    # Create a simple FastMCP server for testing
    server = FastMCP("code-intelligence-minimal")

    @server.tool()
    async def test_connection() -> Dict[str, Any]:
        """Test that the server is working."""
        return {
            "status": "connected",
            "message": "Code Intelligence MCP server is running"
        }

    @server.tool()
    async def get_index_stats() -> Dict[str, Any]:
        """Get current index statistics."""
        return {
            "status": "success",
            "stats": {
                "total_symbols": 0,
                "total_files": 0,
                "total_relationships": 0
            }
        }

    @server.tool()
    async def find_symbol(name: str, file_path: Optional[str] = None) -> Dict[str, Any]:
        """Find a symbol by name."""
        return {
            "status": "success",
            "count": 0,
            "symbols": []
        }

    @server.tool()
    async def find_usages(name: str, file_path: Optional[str] = None) -> Dict[str, Any]:
        """Find all usages of a symbol."""
        return {
            "status": "success",
            "symbol": {
                "name": name,
                "type": "function",
                "file": file_path or "unknown",
                "line": 0
            },
            "usages": [],
            "total_usages": 0
        }

    @server.tool()
    async def add_test_symbol() -> Dict[str, Any]:
        """Add a test symbol to the database."""
        return {
            "status": "success",
            "message": "Test symbol added",
            "symbol_id": "test_123"
        }

    @server.tool()
    async def index_file(file_path: str) -> Dict[str, Any]:
        """Index a single file into the code graph."""
        return {
            "status": "success",
            "file": file_path,
            "symbols_indexed": 0,
            "relationships_indexed": 0,
            "language": "python"
        }

    @server.tool()
    async def semantic_search(query: str, limit: int = 10, symbol_types: Optional[str] = None) -> Dict[str, Any]:
        """Search for code using semantic similarity."""
        return {
            "status": "success",
            "query": query,
            "results": [],
            "total_found": 0
        }

    @server.tool()
    async def find_similar_code(symbol_name: str, file_path: Optional[str] = None) -> Dict[str, Any]:
        """Find code similar to a given symbol."""
        return {
            "status": "success",
            "reference_symbol": {
                "name": symbol_name,
                "type": "function",
                "file": file_path or "unknown",
                "line": 0
            },
            "similar_symbols": [],
            "total_found": 0
        }

    @server.tool()
    async def get_vector_stats() -> Dict[str, Any]:
        """Get vector store statistics."""
        return {
            "status": "success",
            "vector_stats": {
                "total_vectors": 0,
                "collection_name": "code_intelligence"
            }
        }

    @server.tool()
    async def add_repository(name: str, path: str, description: Optional[str] = None, make_active: bool = False) -> Dict[str, Any]:
        """Add a new repository to track."""
        return {
            "status": "success",
            "message": f"Repository '{name}' added successfully",
            "repository_id": "repo_123",
            "path": path
        }

    @server.tool()
    async def list_repositories() -> Dict[str, Any]:
        """List all tracked repositories."""
        return {
            "status": "success",
            "repositories": [],
            "total_count": 0
        }

    @server.tool()
    async def set_active_repository(name: str) -> Dict[str, Any]:
        """Set the active repository for operations."""
        return {
            "status": "success",
            "message": f"Active repository set to '{name}'"
        }

    @server.tool()
    async def index_repository(repository_name: str, include_patterns: Optional[str] = None, exclude_patterns: Optional[str] = None) -> Dict[str, Any]:
        """Index an entire repository."""
        return {
            "status": "success",
            "repository": repository_name,
            "files_indexed": 0,
            "symbols_indexed": 0,
            "relationships_indexed": 0,
            "total_files_found": 0,
            "errors": []
        }

    server.run()


if __name__ == "__main__":
    main()