"""Tests for inventory command."""

from unittest.mock import AsyncMock, MagicMock

import pytest

from mudlib import commands
from mudlib.commands import things  # noqa: F401
from mudlib.player import Player
from mudlib.thing import Thing
from mudlib.zone import Zone


@pytest.fixture
def test_zone():
    """Create a test zone."""
    return Zone(
        name="testzone",
        width=10,
        height=10,
        toroidal=True,
        terrain=[["." for _ in range(10)] for _ in range(10)],
    )


@pytest.fixture
def mock_writer():
    """Create a mock writer."""
    return MagicMock(write=MagicMock(), drain=AsyncMock())


@pytest.fixture
def mock_reader():
    """Create a mock reader."""
    return MagicMock()


@pytest.fixture
def player(mock_reader, mock_writer, test_zone):
    """Create a test player."""
    return Player(
        name="TestPlayer",
        x=5,
        y=5,
        reader=mock_reader,
        writer=mock_writer,
        location=test_zone,
    )


@pytest.mark.asyncio
async def test_inventory_empty(player, mock_writer):
    """Test inventory with no items."""
    from mudlib.commands.things import cmd_inventory

    await cmd_inventory(player, "")

    output = mock_writer.write.call_args_list[-1][0][0]
    assert "You aren't carrying anything." in output


@pytest.mark.asyncio
async def test_inventory_one_item(player, mock_writer):
    """Test inventory with one item."""
    from mudlib.commands.things import cmd_inventory

    Thing(name="rock", location=player)

    await cmd_inventory(player, "")

    output = mock_writer.write.call_args_list[-1][0][0]
    assert "You are carrying:" in output
    assert "rock" in output


@pytest.mark.asyncio
async def test_inventory_multiple_items(player, mock_writer):
    """Test inventory with multiple items."""
    from mudlib.commands.things import cmd_inventory

    Thing(name="rock", location=player)
    Thing(name="sword", location=player)
    Thing(name="shield", location=player)

    await cmd_inventory(player, "")

    output = mock_writer.write.call_args_list[-1][0][0]
    assert "You are carrying:" in output
    assert "rock" in output
    assert "sword" in output
    assert "shield" in output


def test_inventory_command_registered():
    """Test that inventory command is registered."""
    assert "inventory" in commands._registry
    cmd = commands._registry["inventory"]
    assert cmd.name == "inventory"


def test_inventory_alias_registered():
    """Test that 'i' alias is registered."""
    assert "i" in commands._registry
    cmd = commands._registry["i"]
    assert cmd.name == "inventory"
