package privateagent

import (
	"strings"

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

// conversationMessageAuthority keeps the identities involved in a conversation
// send separate at the write boundary:
//   - author is the durable scope_messages author and attachment owner,
//   - routing principal is the member whose authority resolves mentions,
//   - notify principal is an optional human wake/notification subject carried
//     by source adapters.
//
// Service/app deliveries may author as a service member while routing or
// notifying on behalf of a human principal, so callers should not collapse
// these fields into one identity.
type conversationMessageAuthority struct {
	authorMemberID           string
	authorType               domain.MemberType
	routingPrincipalMemberID string
	notifyPrincipalMemberID  string
}

func newConversationMessageAuthority(authorMemberID string, authorType domain.MemberType, options conversationMessageWriteOptions) conversationMessageAuthority {
	routingPrincipalMemberID := strings.TrimSpace(options.RoutingPrincipalMemberID)
	if routingPrincipalMemberID == "" {
		routingPrincipalMemberID = authorMemberID
	}
	return conversationMessageAuthority{
		authorMemberID:           authorMemberID,
		authorType:               authorType,
		routingPrincipalMemberID: routingPrincipalMemberID,
		notifyPrincipalMemberID:  strings.TrimSpace(options.NotifyPrincipalMemberID),
	}
}

func (authority conversationMessageAuthority) authorFreshnessViewerType() string {
	return scopeFreshnessViewerType(authority.authorType)
}
