"""Agent version comparison and install-skip decision."""

from unittest.mock import AsyncMock, patch

import pytest

from server.application.server_batch_common import agent_install_skip_result
from server.domain.models import Server
from server.utils.agent_version import (
    agent_is_installed,
    compute_agent_guidance,
    get_central_agent_version,
    is_version_at_least,
    is_version_lower,
    version_tuple,
)


def test_version_tuple():
    assert version_tuple("2.0.0") == (2, 0, 0)
    assert version_tuple("1.10.3") == (1, 10, 3)
    assert version_tuple("") == ()


def test_is_version_lower():
    assert is_version_lower("1.9.0", "2.0.0") is True
    assert is_version_lower("2.0.0", "2.0.0") is False
    assert is_version_lower("2.1.0", "2.0.0") is False


def test_is_version_at_least():
    assert is_version_at_least("2.0.0", "2.0.0") is True
    assert is_version_at_least("2.0.1", "2.0.0") is True
    assert is_version_at_least("1.9.9", "2.0.0") is False


def test_central_agent_version_matches_agent_py():
    assert get_central_agent_version() == "2.0.0"


def test_agent_is_installed():
    assert agent_is_installed(agent_version_db=None, heartbeat=None) is False
    assert agent_is_installed(agent_version_db="2.0.0", heartbeat=None) is True
    assert agent_is_installed(
        agent_version_db=None,
        heartbeat={"agent_version": "2.0.0", "last_heartbeat": "2026-01-01"},
    ) is True
    assert agent_is_installed(agent_version_db=None, heartbeat={"last_heartbeat": "x"}) is True


def test_compute_agent_guidance_install():
    g = compute_agent_guidance(agent_version_db=None, heartbeat=None, central="2.0.0")
    assert g["agent_action"] == "install"
    assert "尚未安装" in (g["agent_action_message"] or "")


def test_compute_agent_guidance_upgrade():
    g = compute_agent_guidance(
        agent_version_db="1.0.0",
        heartbeat={"agent_version": "1.0.0", "last_heartbeat": "t"},
        central="2.0.0",
    )
    assert g["agent_action"] == "upgrade"


def test_compute_agent_guidance_ok():
    g = compute_agent_guidance(
        agent_version_db="2.0.0",
        heartbeat={"agent_version": "2.0.0", "last_heartbeat": "t", "is_online": "True"},
        central="2.0.0",
    )
    assert g["agent_action"] == "ok"


@pytest.mark.asyncio
async def test_agent_install_skip_when_version_current():
    server = Server(id=1, name="A", agent_api_key="nxs-k", agent_version="2.0.0")
    with patch(
        "server.application.server_batch_common.resolve_remote_agent_version",
        new_callable=AsyncMock,
        return_value="2.0.0",
    ):
        result = await agent_install_skip_result(
            server=server,
            server_id=1,
            server_name="A",
            central_version="2.0.0",
        )
    assert result is not None
    assert result["success"] is True
    assert "已跳过" in result["stdout"]


@pytest.mark.asyncio
async def test_agent_install_no_skip_when_version_lower():
    server = Server(id=2, name="B", agent_api_key="nxs-k", agent_version="1.0.0")
    with patch(
        "server.application.server_batch_common.resolve_remote_agent_version",
        new_callable=AsyncMock,
        return_value="1.0.0",
    ):
        result = await agent_install_skip_result(
            server=server,
            server_id=2,
            server_name="B",
            central_version="2.0.0",
        )
    assert result is None


@pytest.mark.asyncio
async def test_agent_install_no_skip_when_never_installed():
    server = Server(id=3, name="C", agent_api_key=None, agent_version=None)
    result = await agent_install_skip_result(
        server=server,
        server_id=3,
        server_name="C",
        central_version="2.0.0",
    )
    assert result is None
