"""Spawn command for creating mobs."""

from mudlib.commands import CommandDefinition, register
from mudlib.mobs import mob_templates, spawn_mob
from mudlib.player import Player
from mudlib.zone import Zone


async def cmd_spawn(player: Player, args: str) -> None:
    """Spawn a mob at the player's current position."""
    name = args.strip().lower()
    if not name:
        await player.send("Usage: spawn <mob_type>\r\n")
        return

    if name not in mob_templates:
        available = ", ".join(sorted(mob_templates.keys()))
        await player.send(f"Unknown mob type: {name}\r\nAvailable: {available}\r\n")
        return

    if player.location is None or not isinstance(player.location, Zone):
        await player.send("Cannot spawn mob: you are not in a zone.\r\n")
        return

    mob = spawn_mob(mob_templates[name], player.x, player.y, player.location)
    await player.send(f"A {mob.name} appears!\r\n")


register(CommandDefinition("spawn", cmd_spawn, aliases=[], mode="normal"))
