"""FastAPI application for Antragstracker Hagen."""

import os
from pathlib import Path

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

from tracker.api.routes import abstimmungen, ketten, orte, stats, vorlagen

app = FastAPI(
    title="Antragstracker Hagen",
    description="API zur Nachverfolgung kommunaler Anträge und Anfragen",
    version="0.1.0",
)

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

app.include_router(vorlagen.router, prefix="/api")
app.include_router(ketten.router, prefix="/api")
app.include_router(stats.router, prefix="/api")
app.include_router(abstimmungen.router, prefix="/api")
app.include_router(orte.router, prefix="/api")


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


# Serve static frontend files in production
# Try multiple paths (Docker vs local dev)
for static_path in ["/app/static", Path(__file__).parent.parent.parent.parent / "static"]:
    static_dir = Path(static_path)
    if static_dir.exists() and (static_dir / "index.html").exists():
        app.mount("/", StaticFiles(directory=str(static_dir), html=True), name="static")
        break
