public class PhoneClaimCodeProviderService
{
    private Dictionary<string, string> _phoneClaimCodes;
    private Dictionary<string, string> _phoneClaimNumbers;
    private SignalIntegration _signalIntegration;

    public PhoneClaimCodeProviderService(SignalIntegration signalIntegration)
    {
        _phoneClaimCodes = new Dictionary<string, string>();
        _phoneClaimNumbers = new Dictionary<string, string>();
        _signalIntegration = signalIntegration;
    }

    private static Random random = new Random();

    private static string RandomString(int length)
    {
        const string chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
        return new string(Enumerable.Repeat(chars, length)
            .Select(s => s[random.Next(s.Length)]).ToArray());
    }

    public async void GenerateClaimCodeForUserAndNumber(string username, string phoneNumber)
    {
        var generatedCode = string.Empty;
        if (IsCodeGeneratedForUser(username))
        {
            generatedCode = _phoneClaimCodes[username];
        }
        else
        {
            generatedCode = RandomString(6);
            _phoneClaimCodes[username] = generatedCode;
            _phoneClaimNumbers[username] = phoneNumber;
        }

        if (_signalIntegration != null)
        {
            await _signalIntegration.SendMessageToUserAsync("Your phone number validation code is: " + generatedCode, phoneNumber);
        }
    }

    public string ValidateClaimCodeForUser(string code, string username)
    {
        var result = false;
        result = _phoneClaimCodes[username] == code;

        if (result)
        {
            _phoneClaimCodes.Remove(username);
            var number = _phoneClaimNumbers[username];
            _phoneClaimNumbers.Remove(username);
            return number;
        }

        return string.Empty;
    }

    public bool IsCodeGeneratedForUser(string username)
    {
        return _phoneClaimCodes.ContainsKey(username);
    }
}