package user

import (
	"context"
	"crypto/rand"
	"math/big"
)

func (s *service) generateInviteCode(ctx context.Context) string {
	const letters = "ABCDEFGHJKLMNPQRSTUVWXYZ23456789"
	b := make([]byte, 8)
	for i := 0; i < 8; i++ {
		n, _ := rand.Int(rand.Reader, big.NewInt(int64(len(letters))))
		b[i] = letters[n.Int64()]
	}
	code := string(b)
	exists, _ := s.readDB.Users.WithContext(ctx).Where(s.readDB.Users.InviteCode.Eq(code)).First()
	if exists != nil {
		return s.generateInviteCode(ctx)
	}
	return code
}
