from fastapi import FastAPI
from fastapi.responses import FileResponse
from fastapi.middleware.cors import CORSMiddleware
from pathlib import Path

from app.routers import network, alerts, query as query_router, actions as actions_router, emulated_session as emulated_session_router

app = FastAPI(title="P5G Marvis", version="1.0.0", docs_url="/api/docs")

app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],
    allow_methods=["*"],
    allow_headers=["*"],
)

app.include_router(network.router,        prefix="/api")
app.include_router(alerts.router,         prefix="/api")
app.include_router(query_router.router,   prefix="/api")
app.include_router(actions_router.router, prefix="/api")
app.include_router(emulated_session_router.router, prefix="/api")

UI          = Path(__file__).parent / "ui" / "index.html"
OVERVIEW_UI = Path(__file__).parent / "ui" / "overview.html"
TASKS_UI    = Path(__file__).parent / "ui" / "tasks.html"
ACTIONS_UI  = Path(__file__).parent / "ui" / "actions.html"


@app.get("/health")
async def health():
    return {"status": "ok"}


@app.get("/overview")
async def overview_page():
    return FileResponse(str(OVERVIEW_UI))


@app.get("/minis")
async def minis_page():
    return FileResponse(str(TASKS_UI))


@app.get("/tasks")
async def tasks_page():
    return FileResponse(str(TASKS_UI))


@app.get("/actions")
async def actions_page():
    return FileResponse(str(ACTIONS_UI))


# Catch-all: serve the SPA for any unmatched path (supports deep-linking)
@app.get("/{full_path:path}")
async def spa(full_path: str):
    return FileResponse(str(UI))
