package router

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

// ImplicitElapsedQueryGrammar recognises the Russian question shape which asks
// how long it has been without an explicit question word. Word order carries
// the distinction: "давно я не тренировался" asks Maven to look back, while
// "я давно не тренировался" is a statement about the owner.
func ImplicitElapsedQueryGrammar() Grammar {
	return Grammar{Name: "implicit-elapsed-query", Decide: implicitElapsedQueryDecision}
}

func implicitElapsedQueryDecision(utterance string) (Decision, bool) {
	tokens := planTokens(utterance)
	if len(tokens) < 4 || tokens[0] != "давно" || (tokens[1] != "я" && tokens[1] != "мы") {
		return Decision{}, false
	}
	if CarriesCaptureVerb(utterance) || carriesReminderVerbTokens(tokens) {
		return Decision{}, false
	}
	negated := false
	for _, token := range tokens[2:] {
		if token == "не" {
			negated = true
			continue
		}
		if morph.IsVerbForm(token) {
			if !negated {
				return Decision{}, false
			}
			return Decision{
				Stage:      0,
				Intent:     IntentQuery,
				Confidence: 1,
				Source:     SourceRecall,
			}, true
		}
	}
	return Decision{}, false
}
