"""Robot registry and factory helpers."""

from __future__ import annotations

from typing import Optional

from ..core.behavior import RobotBehavior
from ..core.config import LLMConfig, VoiceConfig
from ..core.node import VoiceControlLogger
from ..core.robot import RobotAdapter
from .littlehand.adapter import (
    ALIASES as _littlehand_aliases,
    NAME as _littlehand_name,
    VacuumGripperAdapter,
)
from .littlehand.behavior import LittlehandBehavior


def _resolve_robot_type(robot_type: str) -> tuple[str, bool]:
    """Resolve a robot type or alias to a registered robot name."""
    token = robot_type.strip().lower()
    if token in _littlehand_aliases or token == _littlehand_name:
        return _littlehand_name, True
    return _littlehand_name, False


def get_robot_adapter(robot_type: str, logger: Optional[VoiceControlLogger] = None) -> RobotAdapter:
    """Factory to get robot adapter by type."""
    resolved, known = _resolve_robot_type(robot_type)
    if not known and logger:
        logger.warn(f"Unknown robot type '{robot_type}', defaulting to {resolved}")
    return VacuumGripperAdapter()


def get_behavior(
    robot_type: str,
    config: VoiceConfig,
    llm_config: Optional[LLMConfig] = None,
) -> RobotBehavior:
    """Factory to get robot behavior by type."""
    resolved, _known = _resolve_robot_type(robot_type)
    if resolved == _littlehand_name:
        return LittlehandBehavior(config, VacuumGripperAdapter(), llm_config)
    return LittlehandBehavior(config, VacuumGripperAdapter(), llm_config)


__all__ = [
    "get_robot_adapter",
    "get_behavior",
]
