package enemyevents

import (
	"sort"
	"testing"

	"gitea.maximumdirect.net/eric/notarius/internal/core/source"
	"gitea.maximumdirect.net/eric/notarius/internal/modules/dnd"
	"gitea.maximumdirect.net/eric/notarius/internal/modules/dnd/shared"
)

func TestSupportedKindAndRank(t *testing.T) {
	tests := []struct {
		kind dnd.EnemyEventKind
		rank int
	}{
		{dnd.EnemyEventKindEngaged, 0},
		{dnd.EnemyEventKindIncapacitated, 1},
		{dnd.EnemyEventKindCaptured, 2},
		{dnd.EnemyEventKindFled, 3},
		{dnd.EnemyEventKindKilled, 4},
	}
	for _, test := range tests {
		if rank, ok := KindRank(test.kind); !ok || rank != test.rank || !SupportedKind(test.kind) {
			t.Fatalf("kind %q = (%d, %t), supported %t", test.kind, rank, ok, SupportedKind(test.kind))
		}
	}
	if _, ok := KindRank("unknown"); ok || SupportedKind("unknown") {
		t.Fatal("unsupported kind was accepted")
	}
}

func TestLessOrdersChronologyThenKind(t *testing.T) {
	order := testOrder()
	events := []dnd.EnemyEvent{
		{Name: "Ashfang", Kind: dnd.EnemyEventKindKilled, SourceRefs: refs(20)},
		{Name: "Ashfang", Kind: dnd.EnemyEventKindFled, SourceRefs: refs(20)},
		{Name: "Ashfang", Kind: dnd.EnemyEventKindCaptured, SourceRefs: refs(20)},
		{Name: "Ashfang", Kind: dnd.EnemyEventKindIncapacitated, SourceRefs: refs(20)},
		{Name: "Ashfang", Kind: dnd.EnemyEventKindEngaged, SourceRefs: refs(20)},
		{Name: "Later", Kind: dnd.EnemyEventKindEngaged, SourceRefs: refs(30)},
		{Name: "Earlier", Kind: dnd.EnemyEventKindKilled, SourceRefs: refs(10)},
	}
	sort.SliceStable(events, func(left, right int) bool { return Less(order, events[left], events[right]) })
	want := []dnd.EnemyEventKind{
		dnd.EnemyEventKindKilled,
		dnd.EnemyEventKindEngaged,
		dnd.EnemyEventKindIncapacitated,
		dnd.EnemyEventKindCaptured,
		dnd.EnemyEventKindFled,
		dnd.EnemyEventKindKilled,
		dnd.EnemyEventKindEngaged,
	}
	for index, kind := range want {
		if events[index].Kind != kind {
			t.Fatalf("event %d kind = %q, want %q", index, events[index].Kind, kind)
		}
	}
}

func TestExactEqualUsesSubjectIdentityAndCanonicalEvidence(t *testing.T) {
	order := testOrder()
	first := dnd.EnemyEvent{Name: "  ASHFANG ", Kind: dnd.EnemyEventKindFled, SourceRefs: []source.SourceRef{
		{SourceID: "session", StartUnitID: 30, EndUnitID: 30},
		{SourceID: "session", StartUnitID: 10, EndUnitID: 10},
		{SourceID: "session", StartUnitID: 10, EndUnitID: 10},
	}}
	second := dnd.EnemyEvent{Name: "Ashfang", Kind: dnd.EnemyEventKindFled, SourceRefs: []source.SourceRef{
		{SourceID: "session", StartUnitID: 10, EndUnitID: 10},
		{SourceID: "session", StartUnitID: 30, EndUnitID: 30},
	}}
	if !ExactEqual(order, first, second) || !SourceRefsEqual(order, first.SourceRefs, second.SourceRefs) {
		t.Fatal("canonical duplicate identity was not recognized")
	}
	differentKind := second
	differentKind.Kind = dnd.EnemyEventKindKilled
	differentEvidence := second
	differentEvidence.SourceRefs = []source.SourceRef{{SourceID: "session", StartUnitID: 20, EndUnitID: 20}}
	if ExactEqual(order, first, differentKind) || ExactEqual(order, first, differentEvidence) {
		t.Fatal("distinct event was treated as an exact duplicate")
	}
}

func refs(unitID int) []source.SourceRef {
	return []source.SourceRef{{SourceID: "session", StartUnitID: unitID, EndUnitID: unitID}}
}

func testOrder() shared.SourceRefOrder {
	return shared.NewSourceRefOrder(&source.SourceDocument{ID: "session", Units: []source.SourceUnit{{ID: 10}, {ID: 20}, {ID: 30}}})
}
