"""Small offline command brain for GAR-BOT."""

from __future__ import annotations

from dataclasses import dataclass


@dataclass
class RobotCommand:
    action: str
    value: float = 0.0
    side: str = "both"


class LocalBrain:
    """Rule-based local agent that works without internet.

    This is intentionally predictable for robot safety. A heavier local model
    can be added later as a command suggester, but final actions should still
    pass through this allow-list.
    """

    def parse(self, text: str) -> RobotCommand:
        t = text.strip().lower()
        if not t:
            return RobotCommand("status")
        if any(word in t for word in ["stop", "halt", "emergency", "freeze"]):
            return RobotCommand("stop")
        if "forward" in t:
            return RobotCommand("drive", 0.35)
        if "back" in t or "reverse" in t:
            return RobotCommand("drive", -0.30)
        if "left" in t and "claw" not in t:
            return RobotCommand("turn", -0.30)
        if "right" in t and "claw" not in t:
            return RobotCommand("turn", 0.30)
        if "open" in t and "claw" in t:
            return RobotCommand("claw_open", side=self._side(t))
        if "close" in t and "claw" in t:
            return RobotCommand("claw_close", side=self._side(t))
        if "home" in t and "claw" in t:
            return RobotCommand("claw_home", side=self._side(t))
        return RobotCommand("status")

    def _side(self, text: str) -> str:
        if "left" in text:
            return "left"
        if "right" in text:
            return "right"
        return "both"
