"""Resting system for stamina regeneration."""

from mudlib.commands.movement import send_nearby_message
from mudlib.gmcp import send_char_status, send_char_vitals
from mudlib.player import players

# Stamina regeneration rate: 2.0 per second
# At 10 ticks/sec, that's 0.2 per tick
STAMINA_PER_TICK = 0.2


async def process_resting() -> None:
    """Process stamina regeneration for all resting players.

    Called once per game loop tick (10 times per second).
    Adds STAMINA_PER_TICK stamina to each resting player.
    Sleeping players get 3x the recovery rate.
    Auto-stops resting when stamina reaches max.
    """
    for player in list(players.values()):
        if not player.resting:
            continue

        # Calculate stamina gain (3x if sleeping)
        stamina_gain = STAMINA_PER_TICK * (3 if player.sleeping else 1)
        player.stamina = min(player.stamina + stamina_gain, player.max_stamina)

        # Check if we reached max stamina
        if player.stamina >= player.max_stamina:
            was_sleeping = player.sleeping
            player.resting = False
            player.sleeping = False
            player._last_stamina_cue = 1.0  # Reset stamina cues on full recovery
            send_char_status(player)
            send_char_vitals(player)
            message = (
                "You wake up fully rested.\r\n"
                if was_sleeping
                else "You feel fully rested.\r\n"
            )
            await player.send(message)
            nearby_message = (
                f"{player.name} wakes up.\r\n"
                if was_sleeping
                else f"{player.name} stops resting.\r\n"
            )
            await send_nearby_message(player, player.x, player.y, nearby_message)
