"""Tests for the DARK dream dispatch room."""

from tests.conftest import send


def test_dark_room_exists(game):
    """Verify the DARK room was registered."""
    state, _, _ = game
    dark_room = state.world.get_room("DARK")
    assert dark_room is not None
    assert dark_room.id == "DARK"


def test_dark_entry_clears_inventory(game):
    """Entering the DARK room should strip all inventory items."""
    state, _, _ = game
    # Give player an item
    gown = state.world.get("GOWN")
    gown.move_to(state.protagonist)
    assert len(state.protagonist.children) > 0

    # Enter dark room
    dark_room = state.world.get_room("DARK")
    state.here = dark_room
    state.protagonist.move_to(dark_room)
    if dark_room.action:
        dark_room.action(state, "M-ENTER")
    state.output.flush()

    # Inventory should be cleared (items moved to local_globals)
    assert len(state.protagonist.children) == 0


def test_dark_entry_sets_dreaming(game):
    """Entering DARK should set the dreaming flag."""
    state, _, _ = game
    dark_room = state.world.get_room("DARK")
    state.here = dark_room
    state.protagonist.move_to(dark_room)
    if dark_room.action:
        dark_room.action(state, "M-ENTER")
    state.output.flush()

    assert state.dreaming is True


def test_dark_entry_produces_text(game):
    """Entering DARK should display flavor text."""
    state, _, _ = game
    dark_room = state.world.get_room("DARK")
    state.here = dark_room
    state.protagonist.move_to(dark_room)
    if dark_room.action:
        dark_room.action(state, "M-ENTER")
    text = state.output.flush()

    assert len(text) > 0


def test_dark_dispatch_heart_prob(game):
    """With 100% heart_prob and 0% for others, dispatch should go to ENTRY-BAY."""
    state, _, _ = game
    state.flags["heart_prob"] = 100
    state.flags["vogon_prob"] = 0
    state.flags["traal_prob"] = 0
    state.flags["fleet_prob"] = 0

    # Enter dark room
    dark_room = state.world.get_room("DARK")
    state.here = dark_room
    state.protagonist.move_to(dark_room)
    state.lying_down = False
    if dark_room.action:
        dark_room.action(state, "M-ENTER")
    state.output.flush()

    # Set ready and trigger dispatch
    state.flags["dark_exit_ready"] = True
    state.flags["dark_hint_given"] = True
    text = send(game, "south")

    # Should be dispatched to ENTRY-BAY (Heart of Gold)
    assert state.here.id == "ENTRY-BAY"


def test_dark_dispatch_vogon_prob(game):
    """With 100% vogon_prob and 0% for others, dispatch should go to HOLD."""
    state, _, _ = game
    state.flags["heart_prob"] = 0
    state.flags["vogon_prob"] = 100
    state.flags["traal_prob"] = 0
    state.flags["fleet_prob"] = 0

    # Enter dark room
    dark_room = state.world.get_room("DARK")
    state.here = dark_room
    state.protagonist.move_to(dark_room)
    state.lying_down = False
    if dark_room.action:
        dark_room.action(state, "M-ENTER")
    state.output.flush()

    # Set ready and trigger dispatch
    state.flags["dark_exit_ready"] = True
    state.flags["dark_hint_given"] = True
    text = send(game, "south")

    assert state.here.id == "HOLD"


def test_dark_look_shows_pitch_dark(game):
    """Looking in DARK should describe pitch darkness."""
    state, _, _ = game
    dark_room = state.world.get_room("DARK")
    state.here = dark_room
    state.protagonist.move_to(dark_room)
    if dark_room.action:
        dark_room.action(state, "M-ENTER")
    state.output.flush()

    # Trigger M-LOOK
    dark_room.action(state, "M-LOOK")
    text = state.output.flush()
    assert "pitch dark" in text.lower()
