"""Shared test fixtures for mudlib tests."""

from unittest.mock import AsyncMock, MagicMock

import pytest

from mudlib.combat.engine import active_encounters
from mudlib.player import Player, players
from mudlib.zone import Zone


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


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


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


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


@pytest.fixture
def player(mock_reader, mock_writer, test_zone):
    """Create a test player named Goku at origin."""
    p = Player(name="Goku", x=0, y=0, reader=mock_reader, writer=mock_writer)
    p.location = test_zone
    test_zone._contents.append(p)
    players[p.name] = p
    return p


@pytest.fixture
def nearby_player(mock_reader, mock_writer, test_zone):
    """Create a nearby test player named Vegeta at origin."""
    p = Player(name="Vegeta", x=0, y=0, reader=mock_reader, writer=mock_writer)
    p.location = test_zone
    test_zone._contents.append(p)
    players[p.name] = p
    return p
