"""Unit tests for timmy.vassal.agent_health."""

from __future__ import annotations

import pytest

from timmy.vassal.agent_health import AgentHealthReport, AgentStatus

# ---------------------------------------------------------------------------
# AgentStatus
# ---------------------------------------------------------------------------


def test_agent_status_idle_default():
    s = AgentStatus(agent="claude")
    assert s.is_idle is True
    assert s.is_stuck is False
    assert s.needs_reassignment is False


def test_agent_status_active():
    s = AgentStatus(agent="kimi", active_issue_numbers=[10, 11])
    s.is_idle = len(s.active_issue_numbers) == 0
    assert s.is_idle is False


def test_agent_status_stuck():
    s = AgentStatus(
        agent="claude",
        active_issue_numbers=[7],
        stuck_issue_numbers=[7],
        is_idle=False,
    )
    assert s.is_stuck is True
    assert s.needs_reassignment is True


# ---------------------------------------------------------------------------
# AgentHealthReport
# ---------------------------------------------------------------------------


def test_report_any_stuck():
    claude = AgentStatus(agent="claude", stuck_issue_numbers=[3])
    kimi = AgentStatus(agent="kimi")
    report = AgentHealthReport(agents=[claude, kimi])
    assert report.any_stuck is True


def test_report_all_idle():
    report = AgentHealthReport(agents=[AgentStatus(agent="claude"), AgentStatus(agent="kimi")])
    assert report.all_idle is True


def test_report_for_agent_found():
    kimi = AgentStatus(agent="kimi", active_issue_numbers=[42])
    report = AgentHealthReport(agents=[AgentStatus(agent="claude"), kimi])
    found = report.for_agent("kimi")
    assert found is kimi


def test_report_for_agent_not_found():
    report = AgentHealthReport(agents=[AgentStatus(agent="claude")])
    assert report.for_agent("timmy") is None


# ---------------------------------------------------------------------------
# check_agent_health — no Gitea in unit tests
# ---------------------------------------------------------------------------


@pytest.mark.asyncio
async def test_check_agent_health_unknown_agent():
    """Unknown agent name returns idle status without error."""
    from timmy.vassal.agent_health import check_agent_health

    status = await check_agent_health("unknown-bot")
    assert status.agent == "unknown-bot"
    assert status.is_idle is True


@pytest.mark.asyncio
async def test_check_agent_health_no_token():
    """Returns idle status gracefully when Gitea token is absent."""
    from timmy.vassal.agent_health import check_agent_health

    status = await check_agent_health("claude")
    # Should not raise; returns idle (no active issues discovered)
    assert isinstance(status, AgentStatus)
    assert status.agent == "claude"


@pytest.mark.asyncio
async def test_get_full_health_report_returns_both_agents():
    from timmy.vassal.agent_health import get_full_health_report

    report = await get_full_health_report()
    agent_names = {a.agent for a in report.agents}
    assert "claude" in agent_names
    assert "kimi" in agent_names
