package sourcerelatedness

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"
)

func TestValidatorUsesOnlyCitedTranscriptEvidence(t *testing.T) {
	value := occurrenceList("O'Rin Gate")
	doc := &source.SourceDocument{ID: "session", Units: []source.SourceUnit{{ID: 1, Text: "The party enters o’rin gate."}, {ID: 2, Text: "No location here."}}}
	result, err := New(Options{}).Validate(context.Background(), request(doc, value, contracts.ReferenceSet{}))
	if err != nil || !result.Approved || len(result.Diagnostics) != 0 {
		t.Fatalf("cited match = %#v, %v", result, err)
	}
	value = occurrenceList("Glossary Keep")
	before := cloneList(value)
	references := contracts.ReferenceSet{Slots: map[string]contracts.ResolvedReferenceSlot{"glossary": {Items: []contracts.ReferenceItem{{Content: []byte("Glossary Keep")}}}}}
	result, err = New(Options{}).Validate(context.Background(), request(doc, value, references))
	if err != nil || !result.Approved || len(result.Diagnostics) != 1 || result.Diagnostics[0].ReasonCode != ReasonCode || !strings.Contains(result.Diagnostics[0].Samples[0].Message, "Glossary Keep") || !reflect.DeepEqual(value, before) {
		t.Fatalf("reference-only match = %#v, %v", result, err)
	}
}

func TestValidatorDefersUnreadableEvidenceAndBoundsAdvisories(t *testing.T) {
	invalid := occurrenceList("Missing")
	invalid.Occurrences[0].SourceRefs[0].StartUnitID = 99
	invalid.Occurrences[0].SourceRefs[0].EndUnitID = 99
	result, err := New(Options{}).Validate(context.Background(), request(document(), invalid, contracts.ReferenceSet{}))
	if err != nil || !result.Approved || len(result.Diagnostics) != 0 {
		t.Fatalf("unreadable evidence = %#v, %v", result, err)
	}
	occurrences := make([]dnd.LocationOccurrence, contracts.MaxDiagnosticSamples+2)
	for index := range occurrences {
		occurrences[index] = occurrenceList("Missing").Occurrences[0]
	}
	result, err = New(Options{}).Validate(context.Background(), request(document(), dnd.LocationOccurrenceList{Occurrences: occurrences}, contracts.ReferenceSet{}))
	if err != nil || !result.Approved || len(result.Diagnostics) != 1 || result.Diagnostics[0].OccurrenceCount != len(occurrences) || len(result.Diagnostics[0].Samples) != contracts.MaxDiagnosticSamples {
		t.Fatalf("bounded advisories = %#v, %v", result, err)
	}
}

func TestValidatorRegisters(t *testing.T) {
	registry := pipeline.NewValidatorRegistry()
	if err := Register(registry); err != nil {
		t.Fatal(err)
	}
	if got, ok := registry.Spec(Key); !ok || got != Spec() || got.ExecutionClass != contracts.ExecutionClassDeterministic {
		t.Fatalf("registered spec = %#v, ok = %t", got, ok)
	}
	if got := New(Options{}).CheckpointFingerprints(); len(got) != 1 || got[0].Value != policy {
		t.Fatalf("fingerprints = %#v", got)
	}
}

func request(doc *source.SourceDocument, value dnd.LocationOccurrenceList, references contracts.ReferenceSet) contracts.TypedValidationRequest[dnd.LocationOccurrenceList] {
	return contracts.TypedValidationRequest[dnd.LocationOccurrenceList]{Source: doc, References: references, Value: value}
}
func document() *source.SourceDocument {
	return &source.SourceDocument{ID: "session", Units: []source.SourceUnit{{ID: 1, Text: "The party waits."}}}
}
func occurrenceList(name string) dnd.LocationOccurrenceList {
	return dnd.LocationOccurrenceList{Occurrences: []dnd.LocationOccurrence{{LocationID: "id", Name: name, Kind: dnd.LocationOccurrenceKindVisited, SourceRefs: []source.SourceRef{{SourceID: "session", StartUnitID: 1, EndUnitID: 1}}}}}
}
func cloneList(value dnd.LocationOccurrenceList) dnd.LocationOccurrenceList {
	cloned := dnd.LocationOccurrenceList{Occurrences: append([]dnd.LocationOccurrence(nil), value.Occurrences...)}
	for index := range cloned.Occurrences {
		cloned.Occurrences[index].SourceRefs = append([]source.SourceRef(nil), value.Occurrences[index].SourceRefs...)
	}
	return cloned
}
