package locationregistry

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

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

func TestRegisterPromptAssetsPreparesLocationNormalizationPrompt(t *testing.T) {
	registry := llm.NewAssetRegistry()
	if err := semanticreconcile.RegisterAssets(registry); err != nil {
		t.Fatal(err)
	}
	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-normalize-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: PromptVersion, ProfileID: "location-normalize-test", Inputs: map[string]promptkit.ArtifactRef{"candidates": promptkit.Inline(`{"candidate_number_range":{"first":1,"last":1},"candidates":[{"candidate_number":1,"label":"The Tavern","source_refs":[{"start_unit_id":1,"end_unit_id":1}]}]}`), "transcript": promptkit.Inline(`{"windows":[{"units":[]}]}`)}})
	if err != nil {
		t.Fatal(err)
	}
	if prepared.OutputContract.SchemaPath != "semantic_reconciliation_llm.v1.json" || !strings.Contains(prepared.Messages[1].Content, "candidate_number_range") || !strings.Contains(prepared.Messages[1].Content, "start_unit_id") || !strings.Contains(prepared.Messages[2].Content, "same physical place") || !strings.Contains(prepared.Messages[2].Content, "parent and child places") || !strings.Contains(prepared.Messages[2].Content, "canonical_candidate_number") {
		t.Fatalf("prepared prompt = %#v", prepared)
	}
	for _, index := range []int{2, 4} {
		if prepared.Messages[index].CacheControl == nil || prepared.Messages[index].CacheControl.Type != promptkit.CacheControlEphemeral {
			t.Fatalf("message %d cache = %#v", index, prepared.Messages[index].CacheControl)
		}
	}
	if !strings.Contains(prepared.Messages[3].Content, `"The Tavern"`) || strings.Contains(prepared.Messages[3].Content, `"candidate-`) || strings.Contains(prepared.Messages[3].Content, `"windows"`) {
		t.Fatalf("candidate message = %q", prepared.Messages[3].Content)
	}
	if !strings.Contains(prepared.Messages[4].Content, `"windows"`) || strings.Contains(prepared.Messages[4].Content, `"The Tavern"`) {
		t.Fatalf("transcript message = %q", prepared.Messages[4].Content)
	}
	for index, message := range prepared.Messages {
		if index != 3 && strings.Contains(message.Content, `"The Tavern"`) {
			t.Errorf("message %d unexpectedly rendered candidate input", index)
		}
		if index != 4 && strings.Contains(message.Content, `"windows"`) {
			t.Errorf("message %d unexpectedly rendered transcript input", index)
		}
	}
}
