"""FastAPI app instance for Gateway Bot."""
import logging

from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware

from http_api import router as gateway_router
from http_api_doc import router as doc_router
from daarion_facade.invoke_api import router as invoke_router
from daarion_facade.registry_api import router as registry_router

logging.basicConfig(
    level=logging.INFO,
    format="%(asctime)s [%(levelname)s] %(name)s: %(message)s"
)

app = FastAPI(
    title="Bot Gateway with DAARWIZZ",
    version="1.1.0",
    description="Gateway service for Telegram/Discord bots + DAARION public facade"
)

# CORS for web UI clients (gateway only).
app.add_middleware(
    CORSMiddleware,
    allow_origins=[
        "https://daarion.city",
        "https://www.daarion.city",
        "http://localhost:3000",
    ],
    allow_origin_regex=r"https://.*\.lovable\.app",
    allow_credentials=True,
    allow_methods=["GET", "POST", "OPTIONS"],
    allow_headers=["Authorization", "Content-Type"],
)

# Existing gateway routes.
app.include_router(gateway_router, prefix="", tags=["gateway"])
app.include_router(doc_router, prefix="", tags=["docs"])

# Public facade routes for DAARION.city UI.
app.include_router(registry_router)
app.include_router(invoke_router)


@app.get("/")
async def root():
    return {
        "service": "bot-gateway",
        "version": "1.1.0",
        "agent": "DAARWIZZ",
        "endpoints": [
            "POST /telegram/webhook",
            "POST /discord/webhook",
            "GET /v1/registry/agents",
            "GET /v1/registry/districts",
            "GET /v1/metrics",
            "POST /v1/invoke",
            "GET /v1/jobs/{job_id}",
            "GET /health",
        ]
    }
