package threadcontext

import (
	"strings"

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

// ReplyThreadFacts is the normalized, non-semantic input used by registered
// prompt templates to render reply-thread guidance. This package classifies
// targets only; it does not author prompt prose.
type ReplyThreadFacts struct {
	Target          string
	ReadHistoryTool string
}

// ClassifyReplyThread returns normalized facts for reply-thread targets. It
// does not try to classify message content.
func ClassifyReplyThread(target, readHistoryTool string) (ReplyThreadFacts, bool) {
	target = strings.TrimSpace(target)
	if !isReplyThreadTarget(target) {
		return ReplyThreadFacts{}, false
	}
	return ReplyThreadFacts{Target: target, ReadHistoryTool: strings.TrimSpace(readHistoryTool)}, true
}

func isReplyThreadTarget(target string) bool {
	parsed, err := domain.ParseMessageTargetReference(target)
	if err != nil {
		return false
	}
	if parsed.Kind == domain.MessageTargetKindReplyThread {
		return true
	}
	return parsed.Kind == domain.MessageTargetKindTeam && strings.TrimSpace(parsed.Team.ShortID) != ""
}
