
import asyncio
import httpx
import logging
import sys
import os
from mcp.server.fastmcp import FastMCP

# --- LOGGING SETUP ---
logging.basicConfig(level=logging.INFO, format="[%(levelname)s] %(message)s")
logger = logging.getLogger("orchestrator")

# --- CONFIG ---
WORKER_URL = "http://localhost:3000"

# Set port via environment variable
os.environ["MCP_SERVER_PORT"] = "3000"

# --- MCP SERVER ---
mcp = FastMCP("design-system-orchestrator")

@mcp.tool()
async def ingest_figma_file(file_key: str, token: str) -> str:
    """Ingests a Figma file and extracts design tokens."""
    async with httpx.AsyncClient() as client:
        try:
            resp = await client.post(f"{WORKER_URL}/ingest/figma", json={
                "fileKey": file_key,
                "token": token
            })
            resp.raise_for_status()
            return f"Ingestion Success: {resp.json()}"
        except Exception as e:
            return f"Worker Error: {str(e)}"

@mcp.tool()
async def get_status() -> str:
    """Returns the status of the design system orchestrator."""
    return "Design System Orchestrator: Online"

if __name__ == "__main__":
    logger.info("Starting Design System Orchestrator MCP Server (SSE on port 3000)")
    mcp.run(transport="sse")
