"""Tests for client command."""

from unittest.mock import AsyncMock, MagicMock

import pytest

from mudlib.caps import ClientCaps
from mudlib.commands.help import cmd_client
from mudlib.player import Player, players


@pytest.fixture(autouse=True)
def clear_state():
    players.clear()
    yield
    players.clear()


@pytest.fixture
def mock_writer():
    writer = MagicMock()
    writer.write = MagicMock()
    writer.drain = AsyncMock()
    writer.send_gmcp = MagicMock()
    writer.send_msdp = MagicMock()
    writer.local_option = MagicMock()
    writer.local_option.enabled = MagicMock(return_value=True)
    writer.remote_option = MagicMock()
    writer.remote_option.enabled = MagicMock(return_value=True)
    writer.get_extra_info = MagicMock(
        side_effect=lambda key: {
            "TERM": "TINTIN",
            "cols": 191,
            "rows": 54,
        }.get(key)
    )
    return writer


@pytest.mark.asyncio
async def test_client_shows_protocols(mock_writer):
    """Test client command shows GMCP and MSDP status."""
    p = Player(name="Test", writer=mock_writer)
    p.caps = ClientCaps(ansi=True, truecolor=True)
    await cmd_client(p, "")

    output = mock_writer.write.call_args[0][0]
    assert "GMCP: active" in output
    assert "MSDP: active" in output


@pytest.mark.asyncio
async def test_client_shows_terminal_info(mock_writer):
    """Test client command shows terminal type and size."""
    p = Player(name="Test", writer=mock_writer)
    p.caps = ClientCaps(ansi=True)
    await cmd_client(p, "")

    output = mock_writer.write.call_args[0][0]
    assert "type: TINTIN" in output
    assert "size: 191x54" in output


@pytest.mark.asyncio
async def test_client_shows_color_depth(mock_writer):
    """Test client command shows color depth."""
    p = Player(name="Test", writer=mock_writer)
    p.caps = ClientCaps(ansi=True, truecolor=True, colors_256=True)
    await cmd_client(p, "")

    output = mock_writer.write.call_args[0][0]
    assert "colors: truecolor" in output


@pytest.mark.asyncio
async def test_client_shows_mtts_flags(mock_writer):
    """Test client command shows MTTS capabilities."""
    p = Player(name="Test", writer=mock_writer)
    p.caps = ClientCaps(ansi=True, vt100=True, utf8=True, colors_256=True)
    await cmd_client(p, "")

    output = mock_writer.write.call_args[0][0]
    assert "ANSI" in output
    assert "VT100" in output
    assert "UTF-8" in output
    assert "256 colors" in output


@pytest.mark.asyncio
async def test_client_not_negotiated(mock_writer):
    """Test client command when protocols not negotiated."""
    mock_writer.local_option.enabled.return_value = False
    mock_writer.remote_option.enabled.return_value = False

    p = Player(name="Test", writer=mock_writer)
    p.caps = ClientCaps()
    await cmd_client(p, "")

    output = mock_writer.write.call_args[0][0]
    assert "GMCP: not active" in output
    assert "MSDP: not active" in output


@pytest.mark.asyncio
async def test_client_no_mtts():
    """Test client command with no MTTS capabilities."""
    writer = MagicMock()
    writer.write = MagicMock()
    writer.drain = AsyncMock()
    writer.local_option = MagicMock()
    writer.local_option.enabled = MagicMock(return_value=False)
    writer.remote_option = MagicMock()
    writer.remote_option.enabled = MagicMock(return_value=False)
    writer.get_extra_info = MagicMock(return_value=None)

    p = Player(name="Test", writer=writer)
    p.caps = ClientCaps()
    await cmd_client(p, "")

    output = writer.write.call_args[0][0]
    assert "type: unknown" in output
    assert "MTTS: none detected" in output
