"""Tests for the reload command."""

from pathlib import Path
from unittest.mock import AsyncMock, MagicMock

import pytest

from mudlib.combat import commands as combat_commands
from mudlib.combat.moves import load_moves
from mudlib.commands.reload import cmd_reload
from mudlib.player import Player, players


@pytest.fixture(autouse=True)
def clear_state():
    """Clear players before and after each test."""
    players.clear()
    yield
    players.clear()


@pytest.fixture
def mock_writer():
    writer = MagicMock()
    writer.write = MagicMock()
    writer.drain = AsyncMock()
    return writer


@pytest.fixture
def mock_reader():
    return MagicMock()


@pytest.fixture
def player(mock_reader, mock_writer):
    """Create a test player."""
    p = Player(name="Tester", x=0, y=0, reader=mock_reader, writer=mock_writer)
    players[p.name] = p
    return p


@pytest.fixture
def moves():
    """Load combat moves from content directory."""
    content_dir = Path(__file__).parent.parent / "content" / "combat"
    return load_moves(content_dir)


@pytest.fixture(autouse=True)
def inject_moves(moves):
    """Inject loaded moves into combat commands module."""
    combat_commands.combat_moves = moves
    # Set the combat content dir so reload can find files
    combat_commands.combat_content_dir = (
        Path(__file__).parent.parent / "content" / "combat"
    )
    yield
    combat_commands.combat_moves = {}
    combat_commands.combat_content_dir = None


@pytest.mark.asyncio
async def test_reload_no_args(player, mock_writer):
    """Test reload command with no arguments shows usage."""
    await cmd_reload(player, "")
    assert mock_writer.write.called
    written_text = mock_writer.write.call_args[0][0]
    assert "Usage: reload <name>" in written_text


@pytest.mark.asyncio
async def test_reload_nonexistent(player, mock_writer):
    """Test reload command with nonexistent content."""
    await cmd_reload(player, "nonexistent_file_xyz")
    assert mock_writer.write.called
    written_text = mock_writer.write.call_args[0][0]
    assert "No content found with name: nonexistent_file_xyz" in written_text


@pytest.mark.asyncio
async def test_reload_combat_move(player, mock_writer):
    """Test reloading a combat move TOML."""
    # Reload roundhouse (simple move)
    await cmd_reload(player, "roundhouse")

    # Verify success message
    assert mock_writer.write.called
    written_text = mock_writer.write.call_args[0][0]
    assert "Reloaded combat move: roundhouse" in written_text

    # Verify it's in the combat_moves dict
    from mudlib.combat.commands import combat_moves

    assert "roundhouse" in combat_moves


@pytest.mark.asyncio
async def test_reload_variant_move(player, mock_writer):
    """Test reloading a variant combat move (e.g. punch)."""
    # Reload punch
    await cmd_reload(player, "punch")

    # Verify success message
    assert mock_writer.write.called
    written_text = mock_writer.write.call_args[0][0]
    assert "Reloaded combat move: punch" in written_text

    # Verify variants are in combat_moves
    from mudlib.combat.commands import combat_moves

    assert "punch left" in combat_moves
    assert "punch right" in combat_moves
