"""FastAPI application factory."""
from __future__ import annotations

from fastapi import FastAPI
from fastapi.staticfiles import StaticFiles

from crondag.api.routes.dags import router as dags_router
from crondag.api.routes.runs import router as runs_router
from crondag.api.routes.tasks import router as tasks_router
from crondag.api.routes.health import router as health_router


def create_app() -> FastAPI:
    """Create and configure the FastAPI application."""
    app = FastAPI(
        title="crondag",
        description="Lightweight workflow engine — cron + DAG",
        version="0.1.0",
        docs_url="/api/docs",
        redoc_url="/api/redoc",
    )

    # API routes
    app.include_router(health_router, prefix="/api")
    app.include_router(dags_router, prefix="/api")
    app.include_router(runs_router, prefix="/api")
    app.include_router(tasks_router, prefix="/api")

    # Mount UI routes (imported lazily to avoid circular imports)
    @app.on_event("startup")
    async def _mount_ui():
        from crondag.ui.routes import setup_ui_routes
        setup_ui_routes(app)

    return app
