package utils

import "github.com/google/uuid"

// GenerateID returns a freshly generated random (v4) UUID as a string.
//
// Panics if the system entropy source fails. This matches google/uuid's
// canonical contract (`uuid.New()` and `uuid.NewString()` are defined as
// `Must(NewRandom())`): a failure to read from `crypto/rand.Reader` means
// the kernel entropy source is broken, which is an unrecoverable system
// condition. Continuing with an empty or non-random ID would produce
// silent auth-substrate / data-integrity failures across the 40+ call
// sites that use this helper (JWT JTI minting in `pkg/auth/jwt.go`,
// record IDs in every `Persistence.Insert`, permission junction rows in
// `infrastructure/persistence/perm_*.go`, pack apply rows, bootstrap
// inserts, and password placeholders in `applyUserDefaults`).
//
// Pre-#1425 this function swallowed the error and returned an empty
// string. That created a fail-open in any security-sensitive caller:
// for example, `auth.HashPassword("")` produces a bcrypt hash that
// `auth.VerifyPassword("", hash)` accepts, so a no-Password User insert
// during entropy failure would store a credential anyone can log in
// against. Panicking on entropy failure is strictly safer.
func GenerateID() string {
	return uuid.NewString()
}

// IsValidUUID checks if the string is a valid UUID.
func IsValidUUID(u string) bool {
	_, err := uuid.Parse(u)
	return err == nil
}
