from pathlib import Path

from fastapi import FastAPI
from fastapi.responses import FileResponse, RedirectResponse
from fastapi.staticfiles import StaticFiles

from app.websocket.chat import router as chat_router
from app.websocket.manager import manager

BASE_DIR = Path(__file__).resolve().parent
STATIC_DIR = BASE_DIR / "static"
SLIDES_DIR = BASE_DIR.parent / "docs" / "slides"

app = FastAPI(title="Simple WebSocket Chatroom")

# Registers the WebSocket endpoint:
# ws://localhost:8000/ws/{room_id}/{client_id}
app.include_router(chat_router)

# Serves CSS, JavaScript, font files, and the main HTML page from app/static.
app.mount("/static", StaticFiles(directory=STATIC_DIR), name="static")


@app.get("/slides", include_in_schema=False)
async def slides_redirect():
    """Redirect to the static slides directory so relative asset paths work."""
    return RedirectResponse(url="/slides/")


# Serves the presentation and its relative assets.
# This keeps docs/slides/index.html openable directly from the file system too.
app.mount("/slides", StaticFiles(directory=SLIDES_DIR, html=True), name="slides")


@app.get("/")
async def home():
    """Serve the chatroom GUI."""
    return FileResponse(STATIC_DIR / "index.html")


@app.get("/api/rooms")
async def rooms():
    """Return active rooms and users for simple debugging during the demo."""
    return manager.rooms_overview()


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