#!/usr/bin/env python3
"""
Standalone Firebase MCP server without Django dependency.
This is a simplified version that can be run directly.
"""
import os
import sys
from mcp.server import FastMCP

# Import tools directly
from .tools import auth, firestore, storage


def run_server(transport="stdio"):
    """Run the Firebase MCP server."""
    # Create FastMCP instance
    mcp = FastMCP("Firebase")

    # Register all tools from auth module
    mcp.tool()(auth.verify_id_token)
    mcp.tool()(auth.create_custom_token)
    mcp.tool()(auth.get_user)
    mcp.tool()(auth.delete_user)

    # Register all tools from firestore module
    mcp.tool()(firestore.get_document)
    mcp.tool()(firestore.create_document)
    mcp.tool()(firestore.update_document)
    mcp.tool()(firestore.delete_document)
    mcp.tool()(firestore.list_collections)
    mcp.tool()(firestore.query_collection)

    # Register all tools from storage module
    mcp.tool()(storage.upload_file)
    mcp.tool()(storage.download_file)
    mcp.tool()(storage.delete_file)
    mcp.tool()(storage.list_files)

    # Run the server
    if transport == "stdio":
        print("Starting Firebase MCP server with stdio transport", file=sys.stderr)
        mcp.run(transport="stdio")
    elif transport == "http":
        print("Starting Firebase MCP server with HTTP transport", file=sys.stderr)
        mcp.run(transport="streamable-http")
    else:
        raise ValueError(f"Unsupported transport: {transport}")


if __name__ == '__main__':
    # Check for transport argument
    transport = 'stdio'
    if '--transport' in sys.argv:
        idx = sys.argv.index('--transport')
        if idx + 1 < len(sys.argv):
            transport = sys.argv[idx + 1]

    run_server(transport=transport)