package sourcerefs

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 TestValidatorRequiresExactExtractionChunkIdentity(t *testing.T) {
	doc := document()
	chunk := &source.Chunk{ID: "chunk-1", SourceID: doc.ID, Ref: source.SourceRef{SourceID: doc.ID, StartUnitID: 1, EndUnitID: 2}}
	valid := dnd.SceneDescriptionList{Scenes: []dnd.SceneDescription{{ID: chunk.ID, SourceRef: chunk.Ref, Kind: dnd.SceneKindNarrative, Title: "Arrival", Summary: "The party arrives."}}}
	validator := New(Options{})
	for _, test := range []struct {
		name     string
		request  contracts.TypedValidationRequest[dnd.SceneDescriptionList]
		approved bool
	}{
		{name: "exact extraction identity", request: contracts.TypedValidationRequest[dnd.SceneDescriptionList]{Stage: string(pipeline.StageExtract), Source: doc, Chunk: chunk, Value: valid}, approved: true},
		{name: "wrong ID", request: contracts.TypedValidationRequest[dnd.SceneDescriptionList]{Stage: string(pipeline.StageExtract), Source: doc, Chunk: chunk, Value: dnd.SceneDescriptionList{Scenes: []dnd.SceneDescription{{ID: "other", SourceRef: chunk.Ref, Kind: dnd.SceneKindNarrative, Title: "Arrival", Summary: "The party arrives."}}}}},
		{name: "contained but unequal range", request: contracts.TypedValidationRequest[dnd.SceneDescriptionList]{Stage: string(pipeline.StageExtract), Source: doc, Chunk: chunk, Value: dnd.SceneDescriptionList{Scenes: []dnd.SceneDescription{{ID: chunk.ID, SourceRef: source.SourceRef{SourceID: doc.ID, StartUnitID: 1, EndUnitID: 1}, Kind: dnd.SceneKindNarrative, Title: "Arrival", Summary: "The party arrives."}}}}},
		{name: "missing chunk", request: contracts.TypedValidationRequest[dnd.SceneDescriptionList]{Stage: string(pipeline.StageExtract), Source: doc, Value: valid}},
		{name: "normalization needs no chunk", request: contracts.TypedValidationRequest[dnd.SceneDescriptionList]{Stage: string(pipeline.StageNormalize), Source: doc, Value: valid}, approved: true},
		{name: "invalid current source", request: contracts.TypedValidationRequest[dnd.SceneDescriptionList]{Source: doc, Value: dnd.SceneDescriptionList{Scenes: []dnd.SceneDescription{{ID: "chunk", SourceRef: source.SourceRef{SourceID: doc.ID, StartUnitID: 7, EndUnitID: 7}, Kind: dnd.SceneKindNarrative, Title: "Arrival", Summary: "The party arrives."}}}}},
	} {
		t.Run(test.name, func(t *testing.T) {
			result, err := validator.Validate(context.Background(), test.request)
			if err != nil || result.Approved != test.approved {
				t.Fatalf("Validate() = %#v, %v; want approved=%t", result, err, test.approved)
			}
		})
	}
}

func TestValidatorDefersMalformedShapeAndRegistersPolicy(t *testing.T) {
	result, err := New(Options{}).Validate(context.Background(), contracts.TypedValidationRequest[dnd.SceneDescriptionList]{Value: dnd.SceneDescriptionList{}})
	if err != nil || !result.Approved {
		t.Fatalf("Validate() = %#v, %v; want malformed shape deferred", 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: "They investigate."}}}
}
