"""Chonkie OSS API – FastAPI application entry point.

Run locally::

    chonkie serve

Or with uvicorn directly::

    uvicorn chonkie.api.main:app --reload --port 8000

Or via Docker::

    docker compose up
"""

import os
from contextlib import asynccontextmanager
from importlib.metadata import PackageNotFoundError, version
from typing import AsyncGenerator

from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from starlette.middleware import Middleware

from chonkie.api.database import init_db
from chonkie.api.routes.chunking import router as chunking_router
from chonkie.api.routes.pipelines import router as pipelines_router
from chonkie.api.routes.refineries import router as refineries_router
from chonkie.logger import configure

configure(level=os.getenv("LOG_LEVEL", "INFO"))

try:
    _chonkie_version = version("chonkie")
except PackageNotFoundError:
    _chonkie_version = "unknown"

# CORS is permissive by default for local development.
# Override with the CORS_ORIGINS env var (comma-separated list of origins).
_raw_origins = os.getenv("CORS_ORIGINS", "*")
_origins = [o.strip() for o in _raw_origins.split(",") if o.strip()]


@asynccontextmanager
async def lifespan(app: FastAPI) -> AsyncGenerator[None, None]:
    """Initialize database on startup."""
    await init_db()
    yield


app = FastAPI(
    title="Chonkie OSS API",
    description=(
        "A lightweight, self-hostable REST API that exposes the "
        "[Chonkie](https://github.com/chonkie-inc/chonkie) chunking library "
        "over HTTP.\n\n"
        "No authentication or billing required – just run it and chunk away.\n\n"
        "**Source:** https://github.com/chonkie-inc/chonkie"
    ),
    version=_chonkie_version,
    docs_url="/docs",
    redoc_url="/redoc",
    openapi_url="/openapi.json",
    lifespan=lifespan,
    middleware=[
        Middleware(
            CORSMiddleware,  # type: ignore[arg-type]
            allow_origins=_origins,
            allow_credentials=False,
            allow_methods=["*"],
            allow_headers=["*"],
        )
    ],
)


app.include_router(chunking_router, prefix="/v1")
app.include_router(refineries_router, prefix="/v1")
app.include_router(pipelines_router, prefix="/v1")


@app.get("/health", tags=["Meta"], summary="Health check")
async def health() -> dict:
    """Return a simple alive signal.

    Useful for container health-checks and load-balancer probes.
    """
    return {"status": "ok"}


@app.get("/", tags=["Meta"], summary="API information")
async def root() -> dict:
    """Return basic information about this API instance."""
    return {
        "name": "Chonkie OSS API",
        "version": _chonkie_version,
        "docs": "/docs",
        "health": "/health",
        "chunkers": [
            "/v1/chunk/token",
            "/v1/chunk/sentence",
            "/v1/chunk/recursive",
            "/v1/chunk/semantic",
            "/v1/chunk/code",
        ],
        "refineries": [
            "/v1/refine/embeddings",
            "/v1/refine/overlap",
        ],
        "pipelines": "/v1/pipelines",
    }
