"""SSH task runner service definition.

An ``ssh_tasks`` instance is an SSH endpoint that can run reusable saved tasks.
Tasks themselves stay in the global saved-task registry; the instance only owns
transport (host/port/user/key). Every run is recorded in ``service_task_runs``
and shown as history on the instance's service page.
"""

from __future__ import annotations

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


class SshTasksConfig(ServiceConfigBase):
    """Non-secret SSH task runner config.

    The SSH key itself lives in the saved SSH-key registry and is referenced by
    ``ssh_key_id``. An optional ``passphrase`` is stored as a secret.
    """

    host: str
    port: int = 22
    username: str = ""
    ssh_key_id: str = ""
    timeout_seconds: int = 30


class SshTaskOutputWidgetConfig(WidgetConfigBase):
    """Output of a saved task run on this instance."""

    task_id: str
    # service_id is implicit (the widget's service); allow overriding per-widget.
    service_id: str | None = None


DEFINITION = ServiceDefinition(
    service_type="ssh_tasks",
    name="SSH task runner",
    description="Run reusable saved tasks over SSH and keep run history.",
    config_model=SshTasksConfig,
    secret_fields=[
        SecretField(key="passphrase", label="Key passphrase", helper="Optional"),
    ],
    widget_kinds=[
        widget_kind(
            kind="task_output",
            name="Task output",
            description="Output of a saved task run.",
            model_cls=SshTaskOutputWidgetConfig,
            default_config={"task_id": ""},
            refresh_interval_ms=0,
        ),
    ],
)
