from judy_core.messages import (
    MemberAnswer,
    MemberQuestion,
    TeamMemberId,
    TeamMemberName,
)
from tongcell.core import ActorId, ActorKind, MessageContext

ASK_MEMBER_TOOL_NAME = "ask"

type AskTargets = dict[TeamMemberName, TeamMemberId]


class AskMemberTool:
    def __init__(self, ctx: MessageContext):
        self._ctx = ctx
        self._targets: AskTargets = {}

    def set_ask_targets(self, targets: AskTargets) -> None:
        self._targets = targets

    async def __call__(self, member_name: TeamMemberName, question_text: str):
        """向团队成员(executor, verifier)提问"""
        target_member_id = self._targets.get(member_name)
        if target_member_id is None:
            return "提问失败，成员不存在或者你无权向其提问: {}".format(member_name)

        target_actor_id = ActorId(
            ActorKind(target_member_id.kind), target_member_id.name
        )

        answer = await self._ctx.request(
            MemberQuestion(text=question_text),
            target_actor_id,
            response_type=MemberAnswer,
        )
        return answer.text
