package locationoccurrences

import (
	"context"
	"strings"
	"testing"
	"time"

	"gitea.maximumdirect.net/eric/notarius/internal/framework/llm"
	"gitea.maximumdirect.net/eric/promptkit"
)

func TestRegisterPromptAssetsPreparesLocationOccurrencePrompt(t *testing.T) {
	registry := llm.NewAssetRegistry()
	if err := RegisterPromptAssets(registry); err != nil {
		t.Fatal(err)
	}
	options, err := registry.PromptKitOptions()
	if err != nil {
		t.Fatal(err)
	}
	options = append(options, promptkit.WithProfiles(promptkit.OpenAICompatibleProfile(promptkit.OpenAICompatibleProfileConfig{
		ID: "location-occurrences-test", Endpoint: "http://127.0.0.1:1/v1", Model: "test",
	})))
	engine, err := promptkit.NewEngine(promptkit.Config{Timeout: time.Second}, options...)
	if err != nil {
		t.Fatal(err)
	}
	prepared, err := engine.Prepare(context.Background(), promptkit.RunRequest{
		PromptID: PromptID, PromptVersion: SchemaVersion, ProfileID: "location-occurrences-test",
		Inputs: map[string]promptkit.ArtifactRef{
			"transcript": promptkit.Inline(`{"units":[{"sentinel":"location-occurrence-transcript"}]}`), "players": promptkit.Inline("location-occurrence-player"), "party": promptkit.Inline(" "), "glossary": promptkit.Inline(" "),
			"location_registry": promptkit.Inline(`{"locations":[{"name":"location-occurrence-registry","registry_refs":[{"start_unit_id":7,"end_unit_id":7}],"context":[{"unit_id":7,"text":"registry context"}]}]}`),
		},
	})
	if err != nil {
		t.Fatal(err)
	}
	if prepared.PromptID != PromptID || prepared.OutputContract.SchemaPath != "dnd_location_occurrences_llm.v1.json" {
		t.Fatalf("prepared prompt = %#v", prepared)
	}
	var registryMessage string
	for _, message := range prepared.Messages {
		if strings.Contains(message.Content, "contextual location registry") {
			registryMessage = message.Content
		}
	}
	if !strings.Contains(registryMessage, "location-occurrence-registry") || !strings.Contains(registryMessage, `"registry_refs":[{"start_unit_id":7,"end_unit_id":7}]`) || strings.Contains(registryMessage, "source_id") || strings.Contains(registryMessage, "location:sha256") {
		t.Fatalf("rendered prompt did not preserve source-free registry grounding: %s", registryMessage)
	}
	content := make([]string, len(prepared.Messages))
	for index, message := range prepared.Messages {
		content[index] = message.Content
	}
	rendered := strings.Join(content, "\n")
	policy := strings.ReplaceAll(rendered, "\n", " ")
	if !strings.Contains(policy, "context supports that coreference") || !strings.Contains(policy, "must not create a registry location") || !strings.Contains(policy, "provenance must never replace current-chunk evidence") || !strings.Contains(policy, "complete, ordered") {
		t.Fatalf("rendered prompt = %q, want contextual coreference without registry-derived evidence", rendered)
	}
}
