package services

import "strings"

// Keep the original intent for a name-only lookup, but allow the next search to
// focus on a missing aspect instead of repeatedly returning the same price FAQ.
func hasKnowledgeSearchAspect(query string) bool {
	for _, token := range tokenizeQuery(query) {
		switch token {
		case "harga", "akses", "durasi", "masa", "bayar", "pembayaran", "manfaat", "dapat", "fasilitas", "fitur", "syarat", "retur", "garansi", "pemesanan", "pengiriman", "lokasi", "operasional", "lisensi", "izin", "larangan", "diskon", "promo", "kontak", "tautan", "link", "instalasi":
			return true
		}
	}
	return false
}

// A model may resolve the product correctly but reduce "what do I receive?"
// to just its name. Keep the customer's intent in the retrieval input, without
// adding another model call or replacing the entity resolved from chat history.
func agentKnowledgeQuery(query, question string) string {
	query = clipContextText(strings.TrimSpace(query), 240)
	question = clipContextText(strings.TrimSpace(question), 480)
	if question == "" || strings.EqualFold(query, question) {
		return query
	}
	if strings.Contains(strings.ToLower(question), strings.ToLower(query)) {
		return question
	}
	return query + "\n" + question
}
