"""FastAPI integration for the standardized health check endpoint."""
from typing import Any, List, Optional

from fk_utils.health.base import HealthCheck
from fk_utils.health.healthcheck import check_health


class FastAPIHealthCheck:
    """
    Auto-registers GET /health on a FastAPI application.

    Usage::

        from fk_utils.health.fastapi import FastAPIHealthCheck
        from fk_utils.health import MongoDBCheck, PostgreSQLCheck

        FastAPIHealthCheck(
            app=app,
            environment=settings.ENV,
            country=settings.COUNTRY,
            version=settings.VERSION,
            checks=[
                MongoDBCheck(mongo_client),
                PostgreSQLCheck(session_factory=get_db),
            ],
        )

        # Custom path (e.g. if the service uses a prefix)
        FastAPIHealthCheck(app=app, ..., path="/api/health")
    """

    def __init__(
        self,
        app: Any,
        environment: str,
        country: str,
        version: Optional[str] = None,
        checks: Optional[List[HealthCheck]] = None,
        path: str = "/health",
    ) -> None:
        self._environment = environment
        self._country = country
        self._version = version
        self._checks = checks or []

        app.add_api_route(path, self._endpoint, methods=["GET"])

    async def _endpoint(self) -> dict:
        return check_health(
            environment=self._environment,
            country=self._country,
            version=self._version,
            checks=self._checks,
        )
