// Package messagesend holds the closed vocabulary for the message_send
// target guard: the turn's send scope, its exact targets, and the
// bearer identity binding. Only the message_send guard consumes this
// package; it is not a general invocation or authorization surface.
package messagesend

import (
	"strings"

	"github.com/tidbcloud/AgentTeam/internal/core/domain"
)

// Target is one exact sendable target the runtime delivered in the current
// authoritative request, tagged with where it came from.
type Target struct {
	Target string `json:"target"`
	Source string `json:"source"`
}

// Scope is the turn's send-scope snapshot. An empty scope with ok=true means
// the current turn has no delivered targets, so any valid explicit target is
// allowed.
type Scope struct {
	Targets []Target `json:"targets"`
}

// ContainsTarget reports whether the exact target is in the scope.
func (s Scope) ContainsTarget(target string) bool {
	for _, candidate := range s.Targets {
		if candidate.Target == target {
			return true
		}
	}
	return false
}

// NormalizeTarget normalizes one exact message target with the shared
// domain parser. The scope producer and the message_send sender both use it,
// so an alias or mixed-case delivery target and its normalized copy hit the
// same exact scope entry. It returns "" when the target does not parse.
func NormalizeTarget(target string) string {
	trimmed := strings.TrimSpace(target)
	if trimmed == "" {
		return ""
	}
	reference, err := domain.ParseMessageTargetReference(trimmed)
	if err != nil {
		return ""
	}
	return reference.String()
}
