package copymission

import (
	"strings"

	libkg "haixun-backend/internal/library/knowledge"
)

// ReferenceURLsFromSelection collects evidence/source URLs tied to selected graph nodes.
func ReferenceURLsFromSelection(nodes []libkg.Node, sources []libkg.BraveSource) []string {
	if len(nodes) == 0 {
		return nil
	}
	selectedEvidence := map[string]struct{}{}
	for _, node := range nodes {
		if !node.SelectedForScan {
			continue
		}
		for _, ev := range node.Evidence {
			key := normalizeURLKey(ev.URL)
			if key != "" {
				selectedEvidence[key] = struct{}{}
			}
		}
	}
	if len(selectedEvidence) == 0 {
		return nil
	}

	out := make([]string, 0, len(selectedEvidence))
	seen := map[string]struct{}{}
	appendURL := func(raw string) {
		key := normalizeURLKey(raw)
		if key == "" {
			return
		}
		if _, ok := selectedEvidence[key]; !ok {
			return
		}
		if _, ok := seen[key]; ok {
			return
		}
		seen[key] = struct{}{}
		out = append(out, strings.TrimSpace(raw))
	}
	for _, node := range nodes {
		if !node.SelectedForScan {
			continue
		}
		for _, ev := range node.Evidence {
			appendURL(ev.URL)
		}
	}
	for _, src := range sources {
		appendURL(src.URL)
	}
	return out
}

func normalizeURLKey(raw string) string {
	u := strings.TrimSpace(raw)
	if u == "" {
		return ""
	}
	return strings.ToLower(strings.TrimRight(u, "/"))
}