from services import self_update


def test_is_local_client_accepts_private_and_loopback_addresses():
    assert self_update.is_local_client("127.0.0.1")
    assert self_update.is_local_client("10.0.0.124")
    assert self_update.is_local_client("192.168.1.20")
    assert self_update.is_local_client("::1")
    assert self_update.is_local_client("::ffff:10.0.0.124")


def test_is_local_client_rejects_public_and_empty_hosts():
    assert not self_update.is_local_client("")
    assert not self_update.is_local_client(None)
    assert not self_update.is_local_client("8.8.8.8")
    assert not self_update.is_local_client("example.com")


def test_status_payload_reports_missing_configuration(tmp_path, monkeypatch):
    monkeypatch.setattr(self_update, "is_paramiko_available", lambda: True)

    status = self_update.status_payload(
        {
            "deploy_nas_host": "",
            "deploy_nas_user": "",
            "deploy_nas_password": "",
            "deploy_remote_app_dir": "",
        },
        "10.0.0.124",
    )

    assert status["allowed"] is True
    assert status["configured"] is False
    assert status["available"] is False
    assert status["transport"] == "paramiko"
    assert status["transport_ready"] is True
    assert "Set a NAS host and NAS user first." in (status["reason"] or "")


def test_status_payload_available_when_local_and_configured(monkeypatch):
    monkeypatch.setattr(self_update, "is_paramiko_available", lambda: True)

    status = self_update.status_payload(
        {
            "deploy_nas_host": "MATT-NAS",
            "deploy_nas_user": "ssh",
            "deploy_nas_password": "secret",
            "deploy_remote_app_dir": "/share/Docker/homelabtoolkit",
        },
        "10.0.0.124",
    )

    assert status["allowed"] is True
    assert status["configured"] is True
    assert status["transport"] == "paramiko"
    assert status["transport_ready"] is True
    assert status["password_configured"] is True
    assert status["available"] is True


def test_render_remote_compose_uses_deploy_settings():
    rendered = self_update.render_remote_compose(
        {
            "deploy_remote_app_dir": "/share/Docker/custom-toolkit",
            "deploy_music_host_path": "/share/Movies/Music",
        }
    )

    assert "/share/Docker/custom-toolkit/output:/app/output" in rendered
    assert "/share/Docker/custom-toolkit/cache:/app/cache" in rendered
    assert "/share/Movies/Music:/music" in rendered
    assert "/share/Music:/music" not in rendered
