package scenedescriptions

import (
	"context"
	"encoding/json"
	"strings"
	"testing"
	"time"

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

func TestRegisterPromptAssetsPreparesSceneDescriptionPrompt(t *testing.T) {
	registry := llm.NewAssetRegistry()
	if err := RegisterPromptAssets(registry); err != nil {
		t.Fatalf("RegisterPromptAssets() error = %v, want nil", err)
	}
	options, err := registry.PromptKitOptions()
	if err != nil {
		t.Fatalf("PromptKitOptions() error = %v, want nil", err)
	}
	options = append(options, promptkit.WithProfiles(promptkit.OpenAICompatibleProfile(promptkit.OpenAICompatibleProfileConfig{
		ID: "scene-description-test-profile", Endpoint: "http://127.0.0.1:1/v1", Model: "scene-description-test-model",
	})))
	engine, err := promptkit.NewEngine(promptkit.Config{Timeout: time.Second}, options...)
	if err != nil {
		t.Fatalf("NewEngine() error = %v, want nil", err)
	}
	prepared, err := engine.Prepare(context.Background(), promptkit.RunRequest{
		PromptID: PromptID, PromptVersion: SchemaVersion, ProfileID: "scene-description-test-profile",
		Inputs: map[string]promptkit.ArtifactRef{
			"transcript": promptkit.InlineWithURI("file:///session.json", `{"sentinel":"scene-description-transcript"}`),
			"players":    promptkit.Inline("scene-description-player"),
			"party":      promptkit.Inline(" "),
			"glossary":   promptkit.Inline(" "),
		},
	})
	if err != nil {
		t.Fatalf("Prepare() error = %v, want nil", err)
	}
	if prepared.PromptID != PromptID || prepared.OutputContract.SchemaPath != "dnd_scene_descriptions_llm.v1.json" {
		t.Fatalf("prepared prompt = %#v, want scene-description prompt identity and schema wiring", prepared)
	}
	const combatPolicySentinel = "substantive active combat materially organizes the"
	transcriptIndex := -1
	policyIndex := -1
	instructionsIndex := len(prepared.Messages) - 1
	policyOccurrences := 0
	for index, message := range prepared.Messages {
		if strings.Contains(message.Content, "scene-description-transcript") {
			transcriptIndex = index
		}
		if count := strings.Count(message.Content, combatPolicySentinel); count > 0 {
			policyIndex = index
			policyOccurrences += count
		}
	}
	if policyOccurrences != 1 || policyIndex <= transcriptIndex || policyIndex >= instructionsIndex {
		t.Fatalf("combat policy appeared %d times at message %d; want once after transcript message %d and before instructions message %d", policyOccurrences, policyIndex, transcriptIndex, instructionsIndex)
	}
}

func TestPromptMetadataAndDiagnosticsDoNotContainRawAssets(t *testing.T) {
	hash, err := promptAssetMetadata()
	if err != nil || !strings.HasPrefix(hash, "sha256:") {
		t.Fatalf("promptAssetMetadata() = %q, %v; want hash", hash, err)
	}
	payload, err := json.Marshal(newExtractor(t, &fakeSceneDescriptionsLLMClient{}).ManifestMetadata())
	if err != nil {
		t.Fatal(err)
	}
	for _, forbidden := range []string{"common-dnd-system", "dnd_scene_descriptions_llm.v1.json"} {
		if strings.Contains(string(payload), forbidden) {
			t.Fatalf("metadata leaked raw prompt/schema content %q: %s", forbidden, payload)
		}
	}
}
