"""Unconscious state recovery system."""

from mudlib.gmcp import send_char_status, send_char_vitals
from mudlib.player import players

# Recovery rate per tick (10 ticks/sec)
# 0.1 per tick = 1.0 per second recovery
RECOVERY_PER_TICK = 0.1


async def process_unconscious() -> None:
    """Process recovery for all unconscious players.

    Called once per game loop tick (10 times per second).
    Adds RECOVERY_PER_TICK to both PL and stamina for unconscious players.
    When both PL and stamina are above 0, player regains consciousness.
    """
    for player in list(players.values()):
        # Check if player is unconscious (PL or stamina at or below 0)
        if player.pl > 0 and player.stamina > 0:
            continue

        # Track whether we were unconscious at start
        was_unconscious = player.posture == "unconscious"

        # Recover PL if needed
        if player.pl <= 0:
            player.pl = min(player.pl + RECOVERY_PER_TICK, player.max_pl)

        # Recover stamina if needed
        if player.stamina <= 0:
            player.stamina = min(player.stamina + RECOVERY_PER_TICK, player.max_stamina)

        # Check if player is now conscious
        if was_unconscious and player.pl > 0 and player.stamina > 0:
            # Player regained consciousness
            player._last_stamina_cue = 1.0  # Reset stamina cues on recovery
            send_char_status(player)
            send_char_vitals(player)
            await player.send("You come to.\r\n")
