"""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 typing import TYPE_CHECKING, Any

from media_library_viewer_api.clients.qbittorrent import QbittorrentClient
from media_library_viewer_api.integrations.base import (
    SecretField,
    ServiceBaseUrl,
    ServiceConfigBase,
    ServiceDefinition,
    TestResult,
    WidgetConfigBase,
    translate_connection_error,
    widget_kind,
)

if TYPE_CHECKING:
    from media_library_viewer_api.services.settings_store import SettingsStore


def test_connection(
    config: dict[str, Any],
    secrets: dict[str, str],
    store: SettingsStore,
) -> TestResult:
    """Login + probe maindata; surface auth failures specifically."""
    try:
        base_url = str(config.get("base_url") or "")
        username = str(secrets.get("username") or "")
        password = str(secrets.get("password") or "")
        timeout = int(config.get("timeout_seconds") or 10)
        client = QbittorrentClient(base_url, username, password, timeout=timeout)
        data = client.maindata()
        version = str(data.get("server_state", {}).get("qbittorrent_version", "") or "connected")
        return TestResult(ok=True, detail="Connected to qBittorrent.", evidence=version)
    except RuntimeError as exc:
        lowered = str(exc).lower()
        if "login failed" in lowered:
            return TestResult(ok=False, detail="Authentication failed — qBittorrent rejected the credentials.")
        return translate_connection_error(exc, context="qBittorrent")
    except Exception as exc:
        return translate_connection_error(exc, context="qBittorrent")


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,
        ),
    ],
    test_callable=test_connection,
)
