"""Tests for targeting integration in thing commands."""

import pytest

from mudlib.commands.things import cmd_drop, cmd_get
from mudlib.container import Container
from mudlib.thing import Thing


@pytest.mark.asyncio
async def test_get_prefix_match(player, test_zone):
    """Test that 'get gob' prefix matches 'goblin figurine' on ground."""
    figurine = Thing(name="goblin figurine", x=0, y=0)
    figurine.location = test_zone
    test_zone._contents.append(figurine)

    await cmd_get(player, "gob")

    # Check thing moved to player inventory
    assert figurine in player.contents
    assert figurine not in test_zone._contents


@pytest.mark.asyncio
async def test_get_ordinal_from_ground(player, test_zone):
    """Test that 'get 2.sword' gets second sword from ground."""
    sword1 = Thing(name="sword", x=0, y=0)
    sword1.location = test_zone
    test_zone._contents.append(sword1)

    sword2 = Thing(name="sword", x=0, y=0)
    sword2.location = test_zone
    test_zone._contents.append(sword2)

    await cmd_get(player, "2.sword")

    # Check second sword moved to player inventory
    assert sword2 in player.contents
    assert sword1 not in player.contents


@pytest.mark.asyncio
async def test_drop_prefix_match(player, test_zone):
    """Test that 'drop sw' prefix matches sword in inventory."""
    sword = Thing(name="sword", x=0, y=0)
    sword.move_to(player)

    await cmd_drop(player, "sw")

    # Check sword moved to ground
    assert sword not in player.contents
    assert sword in test_zone._contents
    assert sword.x == 0
    assert sword.y == 0


@pytest.mark.asyncio
async def test_get_ordinal_from_container(player, test_zone):
    """Test that 'get 2.sword from chest' gets second sword from container."""
    chest = Container(name="chest", x=0, y=0, capacity=100)
    chest.location = test_zone
    chest.closed = False
    test_zone._contents.append(chest)

    sword1 = Thing(name="sword", x=0, y=0)
    sword1.move_to(chest)

    sword2 = Thing(name="sword", x=0, y=0)
    sword2.move_to(chest)

    await cmd_get(player, "2.sword from chest")

    # Check second sword moved to player inventory
    assert sword2 in player.contents
    assert sword1 not in player.contents
    assert sword1 in chest.contents


@pytest.mark.asyncio
async def test_get_no_match(player, test_zone):
    """Test that get fails gracefully when no match is found."""
    await cmd_get(player, "nonexistent")

    # Check for error message
    written = "".join(call.args[0] for call in player.writer.write.call_args_list)
    assert "don't see" in written.lower()


@pytest.mark.asyncio
async def test_drop_no_match(player, test_zone):
    """Test that drop fails gracefully when no match is found."""
    await cmd_drop(player, "nonexistent")

    # Check for error message
    written = "".join(call.args[0] for call in player.writer.write.call_args_list)
    assert "not carrying" in written.lower()


@pytest.mark.asyncio
async def test_get_alias_match(player, test_zone):
    """Test that aliases work with targeting."""
    figurine = Thing(name="goblin figurine", x=0, y=0, aliases=["fig", "goblin"])
    figurine.location = test_zone
    test_zone._contents.append(figurine)

    await cmd_get(player, "fig")

    assert figurine in player.contents


@pytest.mark.asyncio
async def test_drop_ordinal(player, test_zone):
    """Test that drop works with ordinal disambiguation."""
    sword1 = Thing(name="sword", x=0, y=0)
    sword1.move_to(player)

    sword2 = Thing(name="sword", x=0, y=0)
    sword2.move_to(player)

    await cmd_drop(player, "2.sword")

    # Check second sword moved to ground
    assert sword2 not in player.contents
    assert sword1 in player.contents
    assert sword2 in test_zone._contents
