from __future__ import annotations


def execute(payload: dict) -> dict:
    robots = payload.get("robots", [])
    eligible = [robot for robot in robots if robot.get("state") == "AVAILABLE"
                and float(robot.get("batteryPercent", 0)) >= 30
                and "fruit_work" in robot.get("capabilities", [])]
    eligible.sort(key=lambda robot: (-float(robot["batteryPercent"]), robot["id"]))
    selected = eligible[0] if eligible else None
    assignment = None if not selected else {"robotId": selected["id"], "taskCount": int(payload.get("taskCount", 0)), "mode": "FRUIT_WORK"}
    return {"component": "via/fruit-multi-robot-control", "accepted": selected is not None,
            "status": "ASSIGNED" if selected else "QUEUED", "target": "service",
            "workOrder": str(payload.get("workOrder", "unknown")), "assignment": assignment,
            "queueReason": None if selected else "NO_ELIGIBLE_ROBOT", "evaluatedRobots": len(robots)}
