"""
main.py
--------
FastAPI application entrypoint.

Run locally:
    uvicorn backend.main:app --reload

Interactive API docs (auto-generated from the Pydantic schemas + routes)
are then available at:
    http://localhost:8000/docs        (Swagger UI)
    http://localhost:8000/redoc       (ReDoc)
"""

import os

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

from .config import settings
from .database import init_db
from .routers import auth_router, chat_router, dashboard_router, reports_router

app = FastAPI(
    title="MindScreen AI",
    description=(
        "Research/educational chatbot for preliminary mental health "
        "screening in young adults (18-30). NOT a diagnostic tool."
    ),
    version="1.0.0",
)

app.add_middleware(
    CORSMiddleware,
    allow_origins=settings.ALLOWED_ORIGINS,
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

app.include_router(auth_router.router)
app.include_router(chat_router.router)
app.include_router(dashboard_router.router)
app.include_router(reports_router.router)


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


@app.get("/health", tags=["meta"])
def health_check():
    return {"status": "ok"}


@app.get("/", tags=["meta"])
def root():
    return {
        "name": "MindScreen AI",
        "disclaimer": "Research/educational screening tool. Not a diagnosis. "
                       "If in crisis, contact local emergency services or a crisis line.",
        "docs": "/docs",
    }


# Serve the plain HTML/CSS/JS frontend from the same app/domain. This means
# a single cPanel "Python App" (and a single subdomain) is enough — no
# separate static hosting or CORS setup needed for same-origin deployments.
_frontend_dir = os.path.join(os.path.dirname(os.path.dirname(__file__)), "frontend")
if os.path.isdir(_frontend_dir):
    app.mount("/app", StaticFiles(directory=_frontend_dir, html=True), name="frontend")
