"""FastAPI application entry point."""
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware

from .config import APP_NAME, APP_VERSION, APP_DESCRIPTION, CORS_ORIGINS
from .database import init_db
from .routers import projects, plans, experience, generation, analytics, ai, search, ai_plan, analysis, adaptive, tasks, monitor, reports

app = FastAPI(
    title=APP_NAME,
    version=APP_VERSION,
    description=APP_DESCRIPTION,
)

# CORS middleware (allow frontend dev server)
app.add_middleware(
    CORSMiddleware,
    allow_origins=CORS_ORIGINS,
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

# Include routers
app.include_router(projects.router)
app.include_router(plans.router)
app.include_router(experience.router)
app.include_router(generation.router)
app.include_router(analytics.router)
app.include_router(ai.router)
app.include_router(search.router)
app.include_router(ai_plan.router)
app.include_router(analysis.router)
app.include_router(adaptive.router)
app.include_router(tasks.router)
app.include_router(monitor.router)
app.include_router(reports.router)


@app.on_event("startup")
def on_startup():
    """Initialize database on startup."""
    init_db()


@app.get("/api/health")
def health_check():
    """Health check endpoint."""
    return {"status": "ok", "app": APP_NAME, "version": APP_VERSION}


@app.get("/")
def root():
    """Root endpoint - redirect to API docs."""
    return {
        "message": APP_NAME,
        "version": APP_VERSION,
        "docs": "/docs",
        "api_health": "/api/health",
    }
