# ================================
# main.py
# ================================
import traceback
import logging
from fastapi import FastAPI
from fastapi.responses import JSONResponse

from routers.message_routes import router, inicializar_mcp  # ← Importas tu router limpio

# LOGGING
logging.basicConfig(level=logging.INFO, format="%(asctime)s [%(levelname)s] %(message)s")
logger = logging.getLogger(__name__)

# FASTAPI APP
app = FastAPI(title="Cliente MULTIAGENTE MCP", root_path="/coop23-mcp")


# --------------------------------
# EVENTOS
# --------------------------------
@app.on_event("startup")
async def startup_event():
    await inicializar_mcp()   # ← Se inicializan agentes y MCP


# --------------------------------
# EXCEPCIONES GLOBALES
# --------------------------------
@app.exception_handler(Exception)
async def global_exception_handler(_, exc: Exception):
    trace = traceback.format_exc()
    logger.error(trace)
    return JSONResponse(status_code=500, content={"detail": "Internal Server Error"})


# --------------------------------
# RUTAS
# --------------------------------
app.include_router(router)
