package discovery

import (
	"strings"
	"sync"

	"github.com/google/uuid"
)

var (
	instanceID          string
	instanceOnce        sync.Once
	instanceIDGenerator = func() string {
		return uuid.NewString()
	}
)

// InstanceID returns a unique, process-stable identifier for the running service instance.
func InstanceID() string {
	instanceOnce.Do(func() {
		instanceID = strings.TrimSpace(instanceIDGenerator())
		if instanceID == "" {
			instanceID = uuid.NewString()
		}
	})
	return instanceID
}
