#!/usr/bin/env python3
# src/chuk_mcp_server/endpoints/info.py
"""
Server information endpoint with comprehensive documentation
"""

import time
import orjson
from starlette.requests import Request
from starlette.responses import Response

from ..protocol import MCPProtocolHandler

INFO_HEADERS = {
    "Access-Control-Allow-Origin": "*",
    "Cache-Control": "public, max-age=300"
}

class InfoEndpoint:
    def __init__(self, protocol_handler: MCPProtocolHandler):
        self.protocol = protocol_handler
    
    async def handle_request(self, request: Request) -> Response:
        if request.method != "GET":
            return Response(
                orjson.dumps({"error": "Method not allowed", "code": 405}),
                status_code=405,
                media_type="application/json",
                headers=INFO_HEADERS
            )
        
        base_url = f"{request.url.scheme}://{request.headers.get('host', 'localhost')}"
        
        info = {
            "server": self.protocol.server_info.model_dump(),
            "capabilities": self.protocol.capabilities.model_dump(),
            "protocol": {
                "version": "MCP 2025-03-26",
                "transport": "HTTP with SSE",
                "inspector_compatible": True
            },
            "framework": {
                "name": "ChukMCPServer",
                "powered_by": "chuk_mcp",
                "optimizations": [
                    "orjson for JSON serialization",
                    "Unix millisecond timestamps",
                    "Pre-computed static responses",
                    "uvloop event loop support",
                    "httptools HTTP parser support"
                ]
            },
            "endpoints": {
                "mcp": f"{base_url}/mcp",
                "health": f"{base_url}/health", 
                "ping": f"{base_url}/ping",
                "version": f"{base_url}/version",
                "info": f"{base_url}/",
                "documentation": f"{base_url}/docs"
            },
            "tools": {
                "count": len(self.protocol.tools),
                "available": list(self.protocol.tools.keys())
            },
            "resources": {
                "count": len(self.protocol.resources),
                "available": list(self.protocol.resources.keys())
            },
            "performance": {
                "ping": "23,000+ RPS",
                "version": "25,000+ RPS", 
                "health": "23,000+ RPS",
                "mcp_protocol": "5,000+ RPS"
            },
            "quick_start": {
                "health_check": f"curl {base_url}/health",
                "ping_test": f"curl {base_url}/ping",
                "version_info": f"curl {base_url}/version"
            }
        }
        
        format_type = request.query_params.get("format", "json").lower()
        
        if format_type == "docs":
            docs = f"""# {info['server']['name']} - ChukMCPServer

**Version:** {info['server']['version']}  
**Protocol:** {info['protocol']['version']}  

## 🚀 Performance Achieved

- **Ping**: {info['performance']['ping']}
- **Version**: {info['performance']['version']}
- **Health**: {info['performance']['health']}
- **MCP Protocol**: {info['performance']['mcp_protocol']}

## 🔗 Endpoints

- **MCP Protocol:** `{info['endpoints']['mcp']}`
- **Health Check:** `{info['endpoints']['health']}`
- **Ping Test:** `{info['endpoints']['ping']}`
- **Version Info:** `{info['endpoints']['version']}`

## 🚀 Quick Test

```bash
{info['quick_start']['ping_test']}
{info['quick_start']['health_check']}
{info['quick_start']['version_info']}
```

**Powered by ChukMCPServer** 🚀
"""
            
            return Response(
                docs,
                media_type="text/markdown",
                headers=INFO_HEADERS
            )
        else:
            return Response(
                orjson.dumps(info),
                media_type="application/json",
                headers=INFO_HEADERS
            )