from fastapi import FastAPI, HTTPException, Request
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import JSONResponse
from pathlib import Path

from app.config import settings
from app.middleware import RequestContextMiddleware
from app.routers import admin, analysis, auth, children, reports, training, videos

CONTRACTS_OPENAPI = Path(__file__).resolve().parents[3] / "contracts" / "openapi.yaml"

app = FastAPI(
    title="Kids AI Posture Platform API",
    version="0.7.0",
    description=(
        "儿童 AI 体态管理平台 API · Sprint 6。"
        "报告复核工作流 + 模型标定 + CI/K8s 部署骨架。"
    ),
)

app.add_middleware(RequestContextMiddleware)

_cors_origins = ["*"] if settings.app_debug else settings.cors_origin_list
app.add_middleware(
    CORSMiddleware,
    allow_origins=_cors_origins,
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

app.include_router(auth.router)
app.include_router(children.router)
app.include_router(videos.router)
app.include_router(analysis.router)
app.include_router(reports.router)
app.include_router(training.router)
app.include_router(admin.router)


@app.exception_handler(HTTPException)
async def http_exception_handler(request: Request, exc: HTTPException):
    if isinstance(exc.detail, dict) and "meta" in exc.detail:
        return JSONResponse(status_code=exc.status_code, content=exc.detail)
    return JSONResponse(status_code=exc.status_code, content={"detail": exc.detail})


@app.get("/health")
def health():
    return {"status": "ok", "env": settings.app_env, "stage": "sprint6"}


@app.get("/")
def root():
    return {
        "name": settings.app_name,
        "docs": "/docs",
        "contract": str(CONTRACTS_OPENAPI),
        "stage": "sprint6-review-ci-k8s",
    }
