package npcregistry

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 TestRegisterPromptAssetsPreparesNormalizationPrompt(t *testing.T) {
	if promptHash, err := promptAssetMetadata(); err != nil || promptHash == "" {
		t.Fatalf("promptAssetMetadata() = %q, %v; want prompt fingerprint", promptHash, err)
	}
	registry := llm.NewAssetRegistry()
	if err := semanticreconcile.RegisterAssets(registry); err != nil {
		t.Fatalf("RegisterAssets() error = %v", err)
	}
	if err := RegisterPromptAssets(registry); err != nil {
		t.Fatalf("RegisterPromptAssets() error = %v", err)
	}
	options, err := registry.PromptKitOptions()
	if err != nil {
		t.Fatalf("PromptKitOptions() error = %v", err)
	}
	options = append(options, promptkit.WithProfiles(promptkit.OpenAICompatibleProfile(promptkit.OpenAICompatibleProfileConfig{
		ID: "normalize-test-profile", Endpoint: "http://127.0.0.1:1/v1", Model: "normalize-test-model",
	})))
	engine, err := promptkit.NewEngine(promptkit.Config{Timeout: time.Second}, options...)
	if err != nil {
		t.Fatalf("NewEngine() error = %v", err)
	}
	prepared, err := engine.Prepare(context.Background(), promptkit.RunRequest{
		PromptID: PromptID, PromptVersion: PromptVersion, ProfileID: "normalize-test-profile",
		Inputs: map[string]promptkit.ArtifactRef{
			"candidates": promptkit.Inline(`{"candidate_number_range":{"first":1,"last":1},"candidates":[{"candidate_number":1,"label":"Mira","source_refs":[{"start_unit_id":1,"end_unit_id":1}]}]}`),
			"transcript": promptkit.Inline(`{"windows":[{"units":[]}]}`),
		},
	})
	if err != nil {
		t.Fatalf("Prepare() error = %v", err)
	}
	if prepared.PromptID != PromptID || prepared.OutputContract.SchemaPath != "semantic_reconciliation_llm.v1.json" {
		t.Fatalf("prepared prompt = %#v, want normalization prompt identity and schema", prepared)
	}
	if prepared.Messages[0].Role != "system" {
		t.Fatalf("initial message role = %q, want system", prepared.Messages[0].Role)
	}
	if !strings.Contains(prepared.Messages[1].Content, "candidate_number") || !strings.Contains(prepared.Messages[1].Content, "integer") {
		t.Fatalf("protocol message = %q, want shared integer-handle protocol", prepared.Messages[1].Content)
	}
	if !strings.Contains(prepared.Messages[2].Content, "same individual") || !strings.Contains(prepared.Messages[2].Content, "canonical_candidate_number") || strings.Contains(prepared.Messages[2].Content, "source ranges") {
		t.Fatalf("NPC policy message = %q, want domain distinctions without copied ranges", prepared.Messages[2].Content)
	}
	for _, index := range []int{2, 4} {
		if cache := prepared.Messages[index].CacheControl; cache == nil || cache.Type != promptkit.CacheControlEphemeral {
			t.Errorf("message %d cache control = %#v, want ephemeral", index, cache)
		}
	}
	for _, index := range []int{0, 1, 3} {
		if cache := prepared.Messages[index].CacheControl; cache != nil {
			t.Errorf("message %d cache control = %#v, want nil", index, cache)
		}
	}
	if !strings.Contains(prepared.Messages[3].Content, `"Mira"`) || strings.Contains(prepared.Messages[3].Content, `"windows"`) {
		t.Fatalf("candidate message = %q, want only rendered candidates", prepared.Messages[3].Content)
	}
	if !strings.Contains(prepared.Messages[4].Content, `"windows"`) || strings.Contains(prepared.Messages[4].Content, `"Mira"`) {
		t.Fatalf("transcript message = %q, want only rendered transcript", prepared.Messages[4].Content)
	}
	for index, message := range prepared.Messages {
		if index != 3 && strings.Contains(message.Content, `"Mira"`) {
			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)
		}
	}
}
