"""qBittorrent service definition.

Declares the config model (base URL + timeout), secret fields (username +
password), and three widget kinds (totals, active, speed). Models on
:mod:`media_library_viewer_api.integrations.prometheus`.
"""

from __future__ import annotations

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


class QbittorrentConfig(ServiceConfigBase):
    """Non-secret qBittorrent connection config."""

    base_url: ServiceBaseUrl
    timeout_seconds: int = 10


class QbittorrentWidgetConfig(WidgetConfigBase):
    """Per-widget config (empty — all three kinds derive from the service connection)."""

    pass


DEFINITION = ServiceDefinition(
    service_type="qbittorrent",
    name="qBittorrent",
    description="Torrent client activity, speeds, and item counts.",
    config_model=QbittorrentConfig,
    secret_fields=[
        SecretField(key="username", label="Username", required=True),
        SecretField(key="password", label="Password", required=True, helper="Stored encrypted"),
    ],
    widget_kinds=[
        widget_kind(
            kind="totals",
            name="Totals",
            description="Count of all listed torrents, broken down by state.",
            model_cls=QbittorrentWidgetConfig,
            default_config={},
            refresh_interval_ms=30_000,
        ),
        widget_kind(
            kind="active",
            name="Active torrents",
            description="Torrents currently downloading or uploading.",
            model_cls=QbittorrentWidgetConfig,
            default_config={},
            refresh_interval_ms=15_000,
        ),
        widget_kind(
            kind="speed",
            name="Speed chart",
            description="Live download/upload speed over a short window.",
            model_cls=QbittorrentWidgetConfig,
            default_config={},
            refresh_interval_ms=5_000,
        ),
    ],
)
