"""Slash command handlers for the Hermes digital pet."""
from __future__ import annotations

import json
from typing import Dict

from .state import apply_event, load_state

_COMMAND_EVENTS: Dict[str, str] = {
    "": "summon",
    "summon": "summon",
    "show": "summon",
    "on": "summon",
    "hide": "hide",
    "off": "hide",
    "wake": "wake",
    "sleep": "sleep",
    "quiet": "sleep",
    "click": "click",
}


def handle_pet_command(raw_args: str = "") -> str:
    parts = (raw_args or "").strip().lower().split()
    command = parts[0] if parts else ""

    if command == "status":
        return json.dumps({"ok": True, "state": load_state().public()}, sort_keys=True)

    if command not in _COMMAND_EVENTS:
        return json.dumps({
            "ok": False,
            "error": "unknown pet command",
            "usage": "/pet [summon|hide|wake|sleep|status|click]",
        }, sort_keys=True)

    state = apply_event(_COMMAND_EVENTS[command], persist=True)
    if state.mode == "waking":
        state = apply_event("wake_complete", state=state, persist=True)
    return json.dumps({"ok": True, "event": _COMMAND_EVENTS[command], "state": state.public()}, sort_keys=True)
