"""Skill unlock checking."""

from mudlib.combat.moves import CombatMove
from mudlib.player import Player


def check_unlocks(player: Player, moves: dict[str, CombatMove]) -> list[str]:
    """Check if player has met unlock conditions for any locked moves.

    Returns list of newly unlocked move names (base command names, not variants).
    """
    newly_unlocked = []

    # Deduplicate by base command (variants share unlock condition)
    seen_commands = set()

    for move in moves.values():
        if move.unlock_condition is None:
            continue

        # Use base command name for unlock tracking
        base = move.command or move.name
        if base in seen_commands:
            continue
        seen_commands.add(base)

        # Already unlocked
        if base in player.unlocked_moves:
            continue

        condition = move.unlock_condition
        unlocked = False

        if condition.type == "kill_count":
            unlocked = player.kills >= condition.threshold
        elif condition.type == "mob_kills":
            count = player.mob_kills.get(condition.mob_name, 0)
            unlocked = count >= condition.threshold

        if unlocked:
            player.unlocked_moves.add(base)
            newly_unlocked.append(base)

    return newly_unlocked
