"""Django 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 DjangoHealthCheck:
    """
    Provides a Django view for the health endpoint.

    Usage (urls.py)::

        from fk_utils.health.django import DjangoHealthCheck
        from fk_utils.health import MongoDBCheck, DjangoPostgreSQLCheck

        health = DjangoHealthCheck(
            environment=settings.ENV,
            country=settings.COUNTRY,
            version=settings.VERSION,
            checks=[
                MongoDBCheck(mongo_client),
                DjangoPostgreSQLCheck(),
            ],
        )

        urlpatterns = [
            path("health", health.view),
        ]
    """

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

    def view(self, request: Any) -> Any:
        from django.http import JsonResponse

        result = check_health(
            environment=self._environment,
            country=self._country,
            version=self._version,
            checks=self._checks,
        )
        return JsonResponse(result)
