"""Tests for character creation flow."""

from collections import deque

import pytest

from mudlib.creation import character_creation


async def make_io(inputs):
    """Create mock read/write functions."""
    input_queue = deque(inputs)
    output = []

    async def read_func():
        if input_queue:
            return input_queue.popleft()
        return None

    async def write_func(msg):
        output.append(msg)

    return read_func, write_func, output


@pytest.mark.asyncio
async def test_character_creation_with_description():
    """Test providing a description returns it."""
    read_func, write_func, output = await make_io(
        ["A wandering swordsman with a mysterious past."]
    )

    result = await character_creation("TestPlayer", read_func, write_func)

    assert result["description"] == "A wandering swordsman with a mysterious past."
    assert any("Character Creation" in msg for msg in output)
    assert any("Description:" in msg for msg in output)
    assert any("A wandering swordsman with a mysterious past." in msg for msg in output)


@pytest.mark.asyncio
async def test_character_creation_empty_input():
    """Test empty input returns empty string."""
    read_func, write_func, output = await make_io([""])

    result = await character_creation("TestPlayer", read_func, write_func)

    assert result["description"] == ""
    assert any("No description set" in msg for msg in output)


@pytest.mark.asyncio
async def test_character_creation_none_input():
    """Test None input (disconnect) returns empty string."""
    read_func, write_func, output = await make_io([None])

    result = await character_creation("TestPlayer", read_func, write_func)

    assert result["description"] == ""
    assert any("No description set" in msg for msg in output)


@pytest.mark.asyncio
async def test_character_creation_prompts():
    """Test the output includes the prompts."""
    read_func, write_func, output = await make_io(["Test description"])

    await character_creation("TestPlayer", read_func, write_func)

    output_text = "".join(output)
    assert "Character Creation" in output_text
    assert "Describe yourself" in output_text
    assert "This is what others see when they look at you" in output_text
    assert "Description:" in output_text


@pytest.mark.asyncio
async def test_character_creation_confirmation():
    """Test the confirmation message includes the description."""
    read_func, write_func, output = await make_io(["A fierce warrior"])

    await character_creation("TestPlayer", read_func, write_func)

    output_text = "".join(output)
    assert "You will be known as:" in output_text
    assert "A fierce warrior" in output_text


@pytest.mark.asyncio
async def test_character_creation_strips_whitespace():
    """Test that leading/trailing whitespace is stripped."""
    read_func, write_func, output = await make_io(["  A nimble thief  "])

    result = await character_creation("TestPlayer", read_func, write_func)

    assert result["description"] == "A nimble thief"
