package router

import "github.com/kami/maven/internal/lexicon"

// PublicCurrentVersionGrammar anchors an explicitly current software/product
// release on the world side. This is the temporal-public analogue of the
// definition grammar: his notes may still be looked up first, but a personal
// boundary scorer must not turn "latest Go version" into private data and stop
// live search.
func PublicCurrentVersionGrammar() Grammar {
	return Grammar{Name: "public-current-version", Decide: publicCurrentVersionDecision}
}

func publicCurrentVersionDecision(utterance string) (Decision, bool) {
	if !IsPublicCurrentVersionQuestion(utterance) {
		return Decision{}, false
	}
	return Decision{
		Stage:      0,
		Intent:     IntentQuery,
		Confidence: 1,
		Source:     SourceWorld,
	}, true
}

// IsPublicCurrentVersionQuestion is the reusable boundary predicate. The two
// required lexical classes describe the frame, while the product/topic stays
// open. Any first-person ownership makes it private/local and declines.
func IsPublicCurrentVersionQuestion(utterance string) bool {
	if !IsQuestionShaped(utterance) {
		return false
	}
	tokens := planTokens(utterance)
	for _, token := range tokens {
		if hasExactWord(lexicon.FirstPerson(), token) || hasExactWord(lexicon.PersonalPossessives(), token) {
			return false
		}
	}
	hasNoun, hasMarker := false, false
	for _, token := range tokens {
		if sameAsAny(token, lexicon.CurrentVersionNouns()) {
			hasNoun = true
		}
		if sameAsAny(token, lexicon.CurrentVersionMarkers()) {
			hasMarker = true
		}
	}
	return hasNoun && hasMarker
}
