package phraser

import (
	"encoding/json"
	"net/http"
	"net/http/httptest"
	"regexp"
	"strings"
	"testing"
)

// unboundedRepeat finds a `*` or `+` applied to a character class or a group.
//
// Those are the two forms that let the model generate without limit. A literal
// `*` inside a quoted terminal is not one, which is why the pattern anchors on
// `]` and `)`.
var unboundedRepeat = regexp.MustCompile(`[\]\)]\s*[*+]`)

// An unbounded ws rule cost 24-30 seconds a turn (Vikunja #531). The model
// opened the JSON object, satisfied `ws ::= [ \t\n]*` with whitespace, and ran
// to the 512-token cap doing it — both interactive turns measured on
// 2026-08-04 decoded exactly 512 tokens of mostly whitespace.
//
// The rule this test enforces is stronger than "ws is bounded", on purpose. A
// grammar is a budget: every repetition in it is something the model is allowed
// to do until the token cap, and the cap is not a design. A new rule with a
// bare `*` is the same defect wearing a different name, and it would cost
// another QA sitting to find.
func TestResponseGrammarHasNoUnboundedRepetition(t *testing.T) {
	for i, line := range strings.Split(responseGrammar, "\n") {
		if m := unboundedRepeat.FindString(line); m != "" {
			t.Errorf("responseGrammar line %d has unbounded repetition %q — bound it, a cap is not a design:\n\t%s",
				i+1, m, strings.TrimSpace(line))
		}
	}
}

// The measured bound. 400 was too tight for the string rule and the comment on
// responseGrammar records why; {0,4} for whitespace was measured the same way,
// three runs stopping cleanly at 33 tokens with no repeat penalty at all.
func TestResponseGrammarWhitespaceIsBounded(t *testing.T) {
	if !strings.Contains(responseGrammar, `ws     ::= [ \t\n]{0,4}`) {
		t.Errorf("the ws rule is not the measured {0,4} bound:\n%s", responseGrammar)
	}
}

// The phrasing request must carry a repeat penalty. It is not what fixes #531 —
// the bounded grammar is — but chatReq having no such field while
// Replier.PhraseReply sent 1.3 is how one phrasing path ran away and the other
// did not. Two wire structs disagreeing about the sampler is not a decision
// anybody made.
func TestPhrasingRequestCarriesARepeatPenalty(t *testing.T) {
	if phraseRepeatPenalty <= 1.0 {
		t.Fatalf("phraseRepeatPenalty is %v, which is the server default and no penalty at all", phraseRepeatPenalty)
	}
	var got []float64
	srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		var req chatReq
		if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
			t.Errorf("decode request: %v", err)
		}
		got = append(got, req.RepeatPenalty)
		w.Header().Set("Content-Type", "application/json")
		w.Write([]byte(`{"choices":[{"message":{"content":"{\"response\": \"ага\", \"mood\": \"neutral\"}"},"finish_reason":"stop"}]}`))
	}))
	defer srv.Close()

	// Every path, not just one: the defect was a whole transport missing the
	// field, so a test that checked a single caller would have passed before
	// the fix as easily as after it.
	callAllPhrasingPaths(t, NewLLMPhraserAt(srv.URL, Config{LLMNudges: true}))

	if len(got) == 0 {
		t.Fatal("no request captured")
	}
	for i, p := range got {
		if p != phraseRepeatPenalty {
			t.Errorf("request %d sent repeat_penalty %v, want %v", i, p, phraseRepeatPenalty)
		}
	}
}
