package facts

// Targets proposes normalized targets from a ClassSummary by picking the most common version.
type Targets struct {
	Kernel     string
	OSImage    string
	Containerd string
	K3sVersion string
	Packages   map[string]string
}

// ChooseTargets picks the highest-count entry for each field. Ties are left empty.
func ChooseTargets(sum *ClassSummary) Targets {
	t := Targets{Packages: map[string]string{}}
	if sum == nil {
		return t
	}
	t.Kernel = topKey(sum.Kernels)
	t.OSImage = topKey(sum.OSImages)
	t.Containerd = topKey(sum.Containerd)
	t.K3sVersion = topKey(sum.K3sVersions)
	for pkg, versions := range sum.PackageStats {
		if v := topKey(versions); v != "" {
			t.Packages[pkg] = v
		}
	}
	return t
}

func topKey(m map[string]int) string {
	best := ""
	bestCount := 0
	for k, c := range m {
		if c > bestCount {
			best = k
			bestCount = c
		} else if c == bestCount {
			// tie: prefer empty to avoid arbitrary pick
			best = ""
		}
	}
	return best
}
