package helpers

import (
	random "crypto/rand"
	"fmt"
	"strings"

	"github.com/google/uuid"
	"math/big"
	"math/rand/v2"
)

func GenerateID() string {
	return uuid.New().String()
}

func GenerateOTP() string {
	num := 100000 + rand.UintN(899999)
	return fmt.Sprintf("%d", num) // 6 digit random number [100,000 - 999,999]
}

func GenerateFastCode() string {
	const letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
	var code string
	for i := 0; i < 6; i++ {
		code += string(letters[rand.UintN(uint(len(letters)))])
	}
	return code
}

func GenerateCashoutID() (string, error) {
	const chars = "abcdefghijklmnopqrstuvwxyz0123456789"
	const length int = 13
	charLen := big.NewInt(int64(len(chars)))
	result := make([]byte, length)

	for i := 0; i < length; i++ {
		index, err := random.Int(random.Reader, charLen)
		if err != nil {
			return "", err
		}
		result[i] = chars[index.Int64()]
	}

	return string(result), nil
}

func MaskPhone(phone string) string {
	if phone == "" {
		return ""
	}
	return phone[:4] + "**" + phone[len(phone)-2:]
}

func MaskEmail(email string) string {
	if email == "" {
		return ""
	}
	return email[:3] + "**" + email[strings.Index(email, "@"):]
}
