package balancer

import (
	"math/rand"

	"github.com/rose_cat707/luminary/internal/model"
)

type KeyCandidate struct {
	Key    model.ProviderKey
	Weight float64
}

func SelectWeighted(candidates []KeyCandidate) *model.ProviderKey {
	if len(candidates) == 0 {
		return nil
	}
	if len(candidates) == 1 {
		k := candidates[0].Key
		return &k
	}
	var total float64
	for _, c := range candidates {
		w := c.Weight
		if w <= 0 {
			w = 1
		}
		total += w
	}
	if total <= 0 {
		k := candidates[0].Key
		return &k
	}
	r := rand.Float64() * total
	var acc float64
	for _, c := range candidates {
		w := c.Weight
		if w <= 0 {
			w = 1
		}
		acc += w
		if r <= acc {
			k := c.Key
			return &k
		}
	}
	k := candidates[len(candidates)-1].Key
	return &k
}
