package shape

import (
	"context"
	"strings"
	"testing"
	"unicode/utf8"

	"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 TestValidatorAcceptsEmptyAndWellFormedEventLists(t *testing.T) {
	for _, value := range []dnd.EnemyEventList{{Events: []dnd.EnemyEvent{}}, validEventList()} {
		result, err := New(Options{}).Validate(context.Background(), contracts.TypedValidationRequest[dnd.EnemyEventList]{Value: value})
		if err != nil || !result.Approved {
			t.Fatalf("Validate() = %#v, %v", result, err)
		}
	}
}

func TestValidatorRejectsOwnedShapeBoundaries(t *testing.T) {
	for _, test := range []struct {
		name   string
		mutate func(*dnd.EnemyEventList)
		want   string
	}{
		{"missing events", func(value *dnd.EnemyEventList) { value.Events = nil }, "events must be present"},
		{"blank name", func(value *dnd.EnemyEventList) { value.Events[0].Name = " \t" }, "name must not be empty"},
		{"unsupported kind", func(value *dnd.EnemyEventList) { value.Events[0].Kind = "unknown" }, "kind is unsupported"},
		{"missing evidence", func(value *dnd.EnemyEventList) { value.Events[0].SourceRefs = nil }, "source_refs must contain"},
	} {
		t.Run(test.name, func(t *testing.T) {
			value := validEventList()
			test.mutate(&value)
			result, err := New(Options{}).Validate(context.Background(), contracts.TypedValidationRequest[dnd.EnemyEventList]{Value: value})
			if err != nil || result.Approved || result.ReasonCode != ReasonCode || !strings.Contains(result.Message, test.want) {
				t.Fatalf("Validate() = %#v, %v; want %q", result, err, test.want)
			}
		})
	}
}

func TestValidatorBoundsDiagnosticsAndRegistersStrictly(t *testing.T) {
	value := dnd.EnemyEventList{Events: make([]dnd.EnemyEvent, 30)}
	for index := range value.Events {
		value.Events[index].Name = strings.Repeat("火", 300)
		value.Events[index].Kind = "invalid"
	}
	result, err := New(Options{}).Validate(context.Background(), contracts.TypedValidationRequest[dnd.EnemyEventList]{Value: value})
	if err != nil || result.Approved || !utf8.ValidString(result.Message) || len([]byte(result.Message)) > 4096 || !strings.Contains(result.Message, "additional issue(s) omitted") {
		t.Fatalf("Validate() = %#v, %v", result, err)
	}
	if got := New(Options{}).CheckpointFingerprints(); len(got) != 1 || got[0].Name != "policy" || got[0].Value != policy {
		t.Fatalf("CheckpointFingerprints() = %#v", got)
	}
	registry := pipeline.NewValidatorRegistry()
	if err := Register(registry); err != nil {
		t.Fatal(err)
	}
	if Spec().Key != Key || Spec().ExecutionClass != contracts.ExecutionClassDeterministic {
		t.Fatalf("Spec() = %#v", Spec())
	}
	if _, err := DecodeOptions(map[string]any{"unexpected": true}); err == nil {
		t.Fatal("DecodeOptions() accepted unknown option")
	}
}

func validEventList() dnd.EnemyEventList {
	return dnd.EnemyEventList{Events: []dnd.EnemyEvent{{
		Name: "Ashfang", Kind: dnd.EnemyEventKindEngaged,
		SourceRefs: []source.SourceRef{{SourceID: "session", StartUnitID: 1, EndUnitID: 1}},
	}}}
}
