package identity

import (
	"context"
	"reflect"
	"strings"
	"testing"

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

func TestValidatorAllowsSameNameAtDistinctEvidence(t *testing.T) {
	refs := []source.SourceRef{{SourceID: "session", StartUnitID: 1, EndUnitID: 1}}
	otherRefs := []source.SourceRef{{SourceID: "session", StartUnitID: 2, EndUnitID: 2}}
	value := dnd.LocationRegistry{Locations: []dnd.Location{
		{ID: domainidentity.DeriveID("Watchtower", refs), Name: "Watchtower", SourceRefs: refs},
		{ID: domainidentity.DeriveID("Watchtower", otherRefs), Name: "Watchtower", SourceRefs: otherRefs},
	}}
	before := value
	result, err := New(Options{}).Validate(context.Background(), request(value))
	if err != nil || !result.Approved || !reflect.DeepEqual(value, before) {
		t.Fatalf("Validate() = %#v, %v; want non-mutating approval", result, err)
	}
}

func TestValidatorDefersShapeAndRejectsDerivationAndDuplicateID(t *testing.T) {
	result, err := New(Options{}).Validate(context.Background(), request(dnd.LocationRegistry{Locations: []dnd.Location{{Name: "Missing"}}}))
	if err != nil || !result.Approved {
		t.Fatalf("shape-invalid result = %#v, %v; want deferral", result, err)
	}
	refs := []source.SourceRef{{SourceID: "session", StartUnitID: 1, EndUnitID: 1}}
	value := dnd.LocationRegistry{Locations: []dnd.Location{
		{ID: "not-an-id", Name: "Gate", SourceRefs: refs},
		{ID: domainidentity.DeriveID("Gate", refs), Name: "Other Gate", SourceRefs: refs},
		{ID: domainidentity.DeriveID("Gate", refs), Name: "Gate", SourceRefs: refs},
	}}
	result, err = New(Options{}).Validate(context.Background(), request(value))
	if err != nil || result.Approved || result.ReasonCode != ReasonCode {
		t.Fatalf("identity result = %#v, %v; want rejection", result, err)
	}
	for _, want := range []string{"invalid_id", "id_mismatch", "duplicate_id", "locations["} {
		if !strings.Contains(result.Message, want) {
			t.Fatalf("message %q missing %q", result.Message, want)
		}
	}
	if !strings.Contains(result.CorrectionGuidance, "duplicate-group proposals") || !strings.Contains(result.CorrectionGuidance, "Gate") || strings.Contains(result.CorrectionGuidance, "not-an-id") || strings.Contains(result.CorrectionGuidance, "locations[") {
		t.Fatalf("CorrectionGuidance = %q, want contextual proposal guidance without IDs or operator paths", result.CorrectionGuidance)
	}
}

func TestValidatorRegistersIdentityPolicy(t *testing.T) {
	if got := New(Options{}).CheckpointFingerprints(); len(got) != 2 || got[0].Value != domainidentity.Policy || got[1].Name != "correction_policy" || got[1].Value != correctionPolicy {
		t.Fatalf("fingerprints = %#v", got)
	}
	registry := pipeline.NewValidatorRegistry()
	if err := Register(registry); err != nil {
		t.Fatalf("Register() error = %v", err)
	}
	if got, ok := registry.Spec(Key); !ok || got != Spec() || got.ExecutionClass != contracts.ExecutionClassDeterministic {
		t.Fatalf("registered spec = %#v, ok = %t", got, ok)
	}
	if _, err := DecodeOptions(map[string]any{"unexpected": true}); err == nil {
		t.Fatal("DecodeOptions() accepted an unknown option")
	}
}

func request(value dnd.LocationRegistry) contracts.TypedValidationRequest[dnd.LocationRegistry] {
	return contracts.TypedValidationRequest[dnd.LocationRegistry]{Value: value}
}
