"""FastAPI adapter for observability endpoints."""

from fastapi import APIRouter, Query, Response

from observabilipy.core.encoding.ndjson import encode_logs, encode_ndjson
from observabilipy.core.encoding.prometheus import encode_current
from observabilipy.core.ports import LogStoragePort, MetricsStoragePort


def create_observability_router(
    log_storage: LogStoragePort,
    metrics_storage: MetricsStoragePort,
) -> APIRouter:
    """Create a FastAPI router with /metrics, /metrics/prometheus, and /logs endpoints.

    Args:
        log_storage: Storage adapter implementing LogStoragePort.
        metrics_storage: Storage adapter implementing MetricsStoragePort.

    Returns:
        APIRouter with /metrics, /metrics/prometheus, and /logs endpoints configured.
    """
    router = APIRouter()

    @router.get("/metrics")
    async def get_metrics(
        since: float = Query(default=0),
    ) -> Response:
        """Return metrics in NDJSON format.

        Args:
            since: Unix timestamp. Returns samples with timestamp > since.
        """
        body = await encode_ndjson(metrics_storage.read(since=since))
        return Response(
            content=body,
            media_type="application/x-ndjson",
        )

    @router.get("/metrics/prometheus")
    async def get_metrics_prometheus() -> Response:
        """Return metrics in Prometheus text format (latest value per metric)."""
        body = await encode_current(metrics_storage.read())
        return Response(
            content=body,
            media_type="text/plain; version=0.0.4; charset=utf-8",
        )

    @router.get("/logs")
    async def get_logs(
        since: float = Query(default=0),
        level: str | None = Query(default=None),
    ) -> Response:
        """Return logs in NDJSON format.

        Args:
            since: Unix timestamp. Returns entries with timestamp > since.
            level: Optional log level filter (case-insensitive).
        """
        body = await encode_logs(log_storage.read(since=since, level=level))
        return Response(
            content=body,
            media_type="application/x-ndjson",
        )

    return router
