package invariants

import (
	"context"
	"reflect"
	"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"
)

func TestValidatorEnforcesOrderDuplicatesAndConflicts(t *testing.T) {
	doc := document()
	first := scene("first", 1, dnd.SceneKindNarrative, "Arrival", "The party arrives.")
	second := scene("second", 2, dnd.SceneKindCombat, "Ambush", "Bandits attack.")
	validator := New(Options{})
	for _, test := range []struct {
		name     string
		value    dnd.SceneDescriptionList
		approved bool
	}{
		{name: "normalized", value: dnd.SceneDescriptionList{Scenes: []dnd.SceneDescription{first, second}}, approved: true},
		{name: "out of order", value: dnd.SceneDescriptionList{Scenes: []dnd.SceneDescription{second, first}}},
		{name: "exact duplicate", value: dnd.SceneDescriptionList{Scenes: []dnd.SceneDescription{first, first}}},
		{name: "same ID conflict", value: dnd.SceneDescriptionList{Scenes: []dnd.SceneDescription{first, scene("first", 2, dnd.SceneKindCombat, "Ambush", "Bandits attack.")}}},
		{name: "same range conflict", value: dnd.SceneDescriptionList{Scenes: []dnd.SceneDescription{first, scene("other", 1, dnd.SceneKindMeta, "Rules", "The table checks rules.")}}},
		{name: "same range same content different ID", value: dnd.SceneDescriptionList{Scenes: []dnd.SceneDescription{scene("a", 1, dnd.SceneKindNarrative, "Arrival", "The party arrives."), scene("b", 1, dnd.SceneKindNarrative, "Arrival", "The party arrives.")}}, approved: true},
	} {
		t.Run(test.name, func(t *testing.T) {
			result, err := validator.Validate(context.Background(), contracts.TypedValidationRequest[dnd.SceneDescriptionList]{Source: doc, Value: test.value})
			if err != nil || result.Approved != test.approved {
				t.Fatalf("Validate() = %#v, %v; want approved=%t", result, err, test.approved)
			}
		})
	}
}

func TestValidatorDefersOwnedFailuresAndRegistersPolicy(t *testing.T) {
	doc := document()
	result, err := New(Options{}).Validate(context.Background(), contracts.TypedValidationRequest[dnd.SceneDescriptionList]{Source: doc, Value: dnd.SceneDescriptionList{}})
	if err != nil || !result.Approved {
		t.Fatalf("Validate() = %#v, %v; want shape deferral", result, err)
	}
	invalidRef := dnd.SceneDescriptionList{Scenes: []dnd.SceneDescription{scene("one", 9, dnd.SceneKindNarrative, "Arrival", "The party arrives.")}}
	result, err = New(Options{}).Validate(context.Background(), contracts.TypedValidationRequest[dnd.SceneDescriptionList]{Source: doc, Value: invalidRef})
	if err != nil || !result.Approved {
		t.Fatalf("Validate() = %#v, %v; want source-reference deferral", result, err)
	}
	registry := pipeline.NewValidatorRegistry()
	if err := Register(registry); err != nil {
		t.Fatal(err)
	}
	if _, ok := registry.Spec(Key); !ok {
		t.Fatalf("registry missing %q", Key)
	}
	if got, want := New(Options{}).CheckpointFingerprints(), []pipeline.CheckpointFingerprint{{Name: "policy", Value: policy}}; !reflect.DeepEqual(got, want) {
		t.Fatalf("fingerprints = %#v, want %#v", got, want)
	}
}

func document() *source.SourceDocument {
	return &source.SourceDocument{ID: "session", Units: []source.SourceUnit{{ID: 1, Text: "The party arrives."}, {ID: 2, Text: "Bandits attack."}}}
}

func scene(id string, unit int, kind dnd.SceneKind, title, summary string) dnd.SceneDescription {
	return dnd.SceneDescription{ID: id, SourceRef: source.SourceRef{SourceID: "session", StartUnitID: unit, EndUnitID: unit}, Kind: kind, Title: title, Summary: summary}
}
