import os
from pathlib import Path

from fastapi import FastAPI
from fastapi.staticfiles import StaticFiles

from pages import router as pages_router, ensure_default_pages
from docker_utils import router as docker_router
from converter import router as converter_router

app = FastAPI(title="µFrame Editor")

app.include_router(converter_router, prefix="/api")
app.include_router(pages_router, prefix="/api")
app.include_router(docker_router, prefix="/api")


@app.on_event("startup")
async def startup():
    ensure_default_pages()


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


# Serve built frontend as static files (SPA fallback)
static_dir = Path(__file__).parent / "static"
if static_dir.is_dir():
    app.mount("/", StaticFiles(directory=str(static_dir), html=True), name="static")
