<?php

namespace App\Helpers;

use Illuminate\Support\Facades\DB;
use Illuminate\Support\Str;

class CodeGenerator
{
    /**
     * Generate a unique 8-digit numeric referral code.
     *
     * @return string
     */
    public static function generateUniqueReferralCode(): string
    {
        do {
            $code = random_int(10000000, 99999999); // 8-digit number
            $code = Str::upper(Str::random(8)); // Generate 8-character code (letters & numbers)
        } while (DB::table('users')->where('referral_code', $code)->exists());

        return (string) $code;
    }
    

    /**
     * Generate a unique 6-digit OTP code.
     *
     * @return int
     */
    public static function generateUniqueOtpCode(): int
    {
        do {
            $code = random_int(10000000, 99999999); // 8-digit number
        } while (DB::table('users')->where('otp', $code)->exists());

        return (string) $code;
    }
}
