package locationregistry

import (
	"reflect"
	"strings"
	"testing"

	"gitea.maximumdirect.net/eric/notarius/internal/framework/contracts"
	"gitea.maximumdirect.net/eric/notarius/internal/framework/pipeline"
	"gitea.maximumdirect.net/eric/notarius/internal/modules/dnd"
	"gitea.maximumdirect.net/eric/notarius/internal/modules/dnd/locations/identity"
)

func TestModuleRegistrationAndMetadata(t *testing.T) {
	if _, err := New(nil, Options{}); err == nil || !strings.Contains(err.Error(), "LLM client") {
		t.Fatalf("New(nil) error = %v, want client rejection", err)
	}
	if _, err := New(&fakeLocationsLLMClient{}, Options{}, contracts.ReferenceSet{}, contracts.ReferenceSet{}); err == nil || !strings.Contains(err.Error(), "at most one") {
		t.Fatalf("New() error = %v, want reference-set rejection", err)
	}
	want := pipeline.ModuleSpec{Key: Key, Stage: pipeline.StageExtract, ExecutionClass: contracts.ExecutionClassLLMBacked, CorrectionProtocol: contracts.CorrectionProtocolSingleResponseV1, Requires: []string{"chunks", "source.transcript"}, Provides: []string{"dnd.location_registry"}, ArtifactKind: dnd.LocationRegistryKind, ReferenceSlots: referenceSlots()}
	if got := ModuleSpec(); !reflect.DeepEqual(got, want) {
		t.Fatalf("ModuleSpec() = %#v, want %#v", got, want)
	}
	registry := pipeline.NewExtractorRegistry()
	if err := Register(registry); err != nil {
		t.Fatalf("Register() error = %v", err)
	}
	if got, ok := registry.Spec(Key); !ok || !reflect.DeepEqual(got, want) {
		t.Fatalf("registry spec = %#v, present = %t", got, ok)
	}
	if err := Register(nil); err == nil || !strings.Contains(err.Error(), "extractor registry") {
		t.Fatalf("Register(nil) error = %v, want registry rejection", err)
	}
	if _, err := DecodeOptions(map[string]any{"unknown": true}); err == nil {
		t.Fatal("DecodeOptions() accepted unknown option")
	}
	extractor := newExtractor(t, &fakeLocationsLLMClient{})
	metadata := extractor.ManifestMetadata()
	for key, value := range map[string]string{
		"prompt_id": PromptID, "prompt_version": SchemaVersion,
		"response_schema_key": string(ResponseSchemaKey), "response_schema_id": ResponseSchemaID,
		"response_schema_name": ResponseSchemaName, "response_schema_version": SchemaVersion,
		"identity_policy": identity.Policy, "mapping_policy": mappingPolicy,
	} {
		if metadata[key] != value {
			t.Fatalf("metadata[%q] = %#v, want %q", key, metadata[key], value)
		}
	}
	if got := extractor.CheckpointFingerprints(); len(got) != 4 || got[0].Name != "prompt" || got[1].Name != "response_schema" || got[2].Value != identity.Policy || got[3].Value != mappingPolicy {
		t.Fatalf("CheckpointFingerprints() = %#v", got)
	}
}
