import json
import os
from pathlib import Path

import httpx
from fastmcp import FastMCP
from fastmcp.server.openapi import RouteMap, MCPType
from httpx import BasicAuth

# Create an HTTP client for your API with basic auth
username = os.getenv("ROX_MCP_USERNAME", "admin")
password = os.getenv("ROX_MCP_PASSWORD", "-")
client = httpx.AsyncClient(base_url="https://mt06301.demos.rox.systems", auth=BasicAuth(username=username, password=password))

# Load your OpenAPI spec from local file
openapi_file = Path(__file__).parent / "acs-openapi-v2.json"
openapi_spec = json.loads(openapi_file.read_text())

# Create the MCP server
mcp = FastMCP.from_openapi(
    openapi_spec=openapi_spec,
    client=client,
    name="ACS API Server",
    route_maps=[
        RouteMap(
            methods=["GET"],
            pattern=r"^/v1/clusters$",
            mcp_type=MCPType.RESOURCE,
        ),
        RouteMap(
            methods=["GET"],
            pattern=r"^/v1/namespaces$",
            mcp_type=MCPType.RESOURCE,
        ),
        RouteMap(
            methods=["GET"],
            pattern=r"^/v1/policies$",
            mcp_type=MCPType.RESOURCE,
        ),
        RouteMap(
            methods=["GET"],
            pattern=r"^/v1/policies/{id}$",
            mcp_type=MCPType.RESOURCE_TEMPLATE,
        ),
        RouteMap(
            methods=["POST", "PUT"],
            pattern=r"^/v1/policies$|^/v1/policies/{id}$",
            mcp_type=MCPType.TOOL,
        ),

        # Exclude all remaining routes
        RouteMap(mcp_type=MCPType.EXCLUDE),
    ],

)

if __name__ == "__main__":
    mcp.run()
