package mcp

import (
	"log/slog"
	"strings"
)

// confirmAcceptedVisibleReply settles only from a genuine successful response's
// canonical TargetKey and SeenUpToSeq. AcceptedSeq, local snapshots, and wake
// tokens are not authoritative target coverage.
func (h *mcpHandler) confirmAcceptedVisibleReply(agentID, target string, result agentSendMessageResponse, replyMessageID string) {
	if h == nil || h.c == nil || h.runtime == nil {
		return
	}
	if result.State != agentSendStateSent && result.State != agentSendStateAlreadySent {
		return
	}
	targetKey := strings.TrimSpace(result.TargetKey)
	if targetKey == "" || result.SeenUpToSeq < 0 {
		return
	}
	confirmer, ok := h.runtime.(targetAwareVisibleReplyConfirmer)
	if !ok {
		return
	}
	if !confirmer.ConfirmVisibleReplyForTarget(agentID, targetKey, result.SeenUpToSeq, replyMessageID) {
		slog.Warn("agentteam message_send target-frontier reply confirmation was rejected",
			"agent_id", agentID,
			"target", target,
			"target_key", targetKey,
			"seen_up_to_seq", result.SeenUpToSeq,
			"reply_message_id", replyMessageID,
		)
	}
}
