"""Verb handlers for object interactions."""


async def unlock_handler(obj, player, args):
    """
    Handle unlocking a container.

    Args:
        obj: The object being unlocked (should be a Container)
        player: The player attempting to unlock
        args: Additional arguments (unused)
    """
    from mudlib.container import Container

    if not isinstance(obj, Container):
        await player.send("That can't be unlocked.\r\n")
        return

    if not obj.locked:
        await player.send("That's not locked.\r\n")
        return

    # Check for key in player inventory
    key = None
    for item in player.contents:
        if item.name.lower() == "key" or "key" in [
            a.lower() for a in getattr(item, "aliases", [])
        ]:
            key = item
            break

    if key is None:
        await player.send("You don't have a key.\r\n")
        return

    obj.locked = False
    obj.closed = False
    await player.send(f"You unlock the {obj.name} with the {key.name}.\r\n")
