"""Random baseline agent for AnestheSim environments."""

from __future__ import annotations

import gymnasium as gym
import numpy as np


def evaluate_random(
    env_id: str,
    n_episodes: int = 50,
    seed: int = 42,
) -> dict:
    """Evaluate a random agent on the given environment.

    Args:
        env_id: Gymnasium environment ID.
        n_episodes: Number of evaluation episodes.
        seed: Random seed.

    Returns:
        Dict with mean_reward, std_reward, episode_rewards.
    """
    import anesthesim  # noqa: F401 - triggers env registration

    env = gym.make(env_id)
    rng = np.random.default_rng(seed)
    rewards_list: list[float] = []

    for ep in range(n_episodes):
        obs, _ = env.reset(seed=int(rng.integers(0, 2**31)))
        total_reward = 0.0
        done = False
        while not done:
            action = env.action_space.sample()
            obs, reward, terminated, truncated, info = env.step(action)
            total_reward += reward
            done = terminated or truncated
        rewards_list.append(total_reward)

    env.close()
    return {
        "mean_reward": float(np.mean(rewards_list)),
        "std_reward": float(np.std(rewards_list)),
        "episode_rewards": rewards_list,
    }
