package npcoccurrences

import (
	"fmt"
	"sort"

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

type orderedOccurrenceResponse struct {
	value       occurrenceResponse
	earliest    int
	hasEvidence bool
}

func canonicalizeResponse(response *extractionResponse, order shared.SourceRefOrder, sourceID string) {
	if response == nil {
		return
	}
	ordered := make([]orderedOccurrenceResponse, len(response.Occurrences))
	for index := range response.Occurrences {
		earliest, hasEvidence := canonicalizeOccurrence(&response.Occurrences[index], order, sourceID)
		ordered[index] = orderedOccurrenceResponse{
			value:       response.Occurrences[index],
			earliest:    earliest,
			hasEvidence: hasEvidence,
		}
	}
	sort.SliceStable(ordered, func(i, j int) bool {
		if ordered[i].hasEvidence != ordered[j].hasEvidence {
			return ordered[i].hasEvidence
		}
		if !ordered[i].hasEvidence {
			return false
		}
		return ordered[i].earliest < ordered[j].earliest
	})
	for index := range ordered {
		response.Occurrences[index] = ordered[index].value
	}
}

func canonicalizeOccurrence(occurrence *occurrenceResponse, order shared.SourceRefOrder, sourceID string) (int, bool) {
	if occurrence == nil {
		return 0, false
	}
	refs := order.Canonicalize(order.OrderEndpoints(canonicalSourceRefs(occurrence.SourceRefs, sourceID)))
	occurrence.SourceRefs = occurrenceResponseRefs(refs)
	return order.EarliestValid(refs)
}

func canonicalOccurrenceList(response extractionResponse, sourceID string, registry *npcregistry.Registry) (dnd.NPCOccurrenceList, error) {
	occurrences := make([]dnd.NPCOccurrence, len(response.Occurrences))
	for index, occurrence := range response.Occurrences {
		canonical, ok := registry.Lookup(occurrence.Name)
		if !ok {
			return dnd.NPCOccurrenceList{}, fmt.Errorf("occurrences[%d].name is not in the NPC registry", index)
		}
		occurrences[index] = dnd.NPCOccurrence{
			NPCID:      canonical.ID,
			Name:       canonical.Name,
			Kind:       dnd.NPCOccurrenceKind(occurrence.Kind),
			SourceRefs: canonicalSourceRefs(occurrence.SourceRefs, sourceID),
		}
	}
	if response.Occurrences == nil {
		occurrences = nil
	}
	return dnd.NPCOccurrenceList{Occurrences: occurrences}, nil
}

func canonicalSourceRefs(refs []occurrenceSourceRefResponse, sourceID string) []source.SourceRef {
	if refs == nil {
		return nil
	}
	out := make([]source.SourceRef, len(refs))
	for index, ref := range refs {
		out[index] = source.SourceRef{SourceID: sourceID, StartUnitID: ref.StartUnitID, EndUnitID: ref.EndUnitID}
	}
	return out
}

func occurrenceResponseRefs(refs []source.SourceRef) []occurrenceSourceRefResponse {
	if refs == nil {
		return nil
	}
	values := make([]occurrenceSourceRefResponse, len(refs))
	for index, ref := range refs {
		values[index] = occurrenceSourceRefResponse{StartUnitID: ref.StartUnitID, EndUnitID: ref.EndUnitID}
	}
	return values
}
