from dataclasses import dataclass
from typing import Dict

from ..config import DemoConfig
from ..infra.outbound.message_action_runner import RunMessageActionParams, run_message_action
from ..runtime import Runtime


@dataclass
class MessageResult:
    data: Dict[str, object]


def message_command(opts: Dict[str, object], runtime: Runtime, cfg: DemoConfig) -> MessageResult:
    raw_action = str(opts.get("action", "send")).strip().lower()
    if raw_action not in ("send", "poll", "broadcast", "react", "delete"):
        raise ValueError(f"Unknown message action: {raw_action}")
    result = run_message_action(
        RunMessageActionParams(
            cfg=cfg,
            action=raw_action,  # type: ignore[arg-type]
            params=opts,
            dry_run=bool(opts.get("dry_run", False)),
        )
    )
    runtime.log(f"[{result['handled_by']}] {result['action']} on {result['channel']}")
    return MessageResult(data=result)

