"""Prometheus service definition."""

from __future__ import annotations

from media_library_viewer_api.integrations.base import (
    SecretField,
    ServiceBaseUrl,
    ServiceConfigBase,
    ServiceDefinition,
    WidgetConfigBase,
    widget_kind,
)


class PrometheusConfig(ServiceConfigBase):
    """Non-secret Prometheus connection config."""

    base_url: ServiceBaseUrl
    timeout_seconds: int = 10


class PrometheusMetricWidgetConfig(WidgetConfigBase):
    """A PromQL instant query rendered as a metric."""

    promql: str


class PrometheusChartWidgetConfig(WidgetConfigBase):
    """A PromQL range query rendered as a multi-series line chart (SC-101..SC-104)."""

    promql: str
    window: str = "1h"  # one of 1h / 6h / 24h / 7d (see WINDOW_PRESETS)


class PrometheusGaugeWidgetConfig(WidgetConfigBase):
    """A PromQL instant query rendered as a gauge with optional threshold bands (SC-109..SC-111)."""

    promql: str
    warn_at: float | None = None
    crit_at: float | None = None
    min: float | None = None
    max: float | None = None
    unit: str | None = None


class PrometheusMeanWidgetConfig(WidgetConfigBase):
    """A PromQL range query averaged client-side into a single value (SC-112..SC-114)."""

    promql: str
    window: str = "1h"  # one of 1h / 6h / 24h / 7d (see WINDOW_PRESETS)
    unit: str | None = None


DEFINITION = ServiceDefinition(
    service_type="prometheus",
    name="Prometheus",
    description="Metrics storage and PromQL queries.",
    config_model=PrometheusConfig,
    secret_fields=[
        SecretField(key="api_key", label="API key", helper="Optional bearer token"),
    ],
    widget_kinds=[
        widget_kind(
            kind="metric",
            name="Metric",
            description="Instant query result rendered as a metric.",
            model_cls=PrometheusMetricWidgetConfig,
            default_config={"promql": ""},
            refresh_interval_ms=30_000,
        ),
        widget_kind(
            kind="chart",
            name="Chart",
            description="Multi-series line chart from a PromQL range query.",
            model_cls=PrometheusChartWidgetConfig,
            default_config={"promql": "", "window": "1h"},
            refresh_interval_ms=60_000,
        ),
        widget_kind(
            kind="gauge",
            name="Gauge",
            description="Instant query rendered as a gauge with optional threshold bands.",
            model_cls=PrometheusGaugeWidgetConfig,
            default_config={"promql": ""},
            refresh_interval_ms=30_000,
        ),
        widget_kind(
            kind="mean",
            name="Mean",
            description="Average value of a PromQL query over a time window.",
            model_cls=PrometheusMeanWidgetConfig,
            default_config={"promql": "", "window": "1h"},
            refresh_interval_ms=60_000,
        ),
    ],
)
