using System.Security.Cryptography;

namespace CMSMicroservice.Application.Common.Extensions;
public static class UtilExtensions
{
    public static string NormalizeIranMobile(this string input)
    {
        if (string.IsNullOrWhiteSpace(input)) throw new ArgumentException("mobile is empty");
        var m = new string(input.Where(char.IsDigit).ToArray());

        if (m.StartsWith("0098")) m = m[4..];
        else if (m.StartsWith("098")) m = m[3..];
        else if (m.StartsWith("98")) m = m[2..];
        if (!m.StartsWith("0")) m = "0" + m;

        if (m.Length != 11 || !m.StartsWith("09"))
            throw new ArgumentException("شماره موبایل نامعتبر است.");
        return m;
    }
    public static string Generate(int digits = 10, bool firstDigitNonZero = false)
    {
        if (digits <= 0) throw new ArgumentOutOfRangeException(nameof(digits));

        var chars = new char[digits];

        // رقم اول
        if (firstDigitNonZero)
            chars[0] = (char)('0' + RandomNumberGenerator.GetInt32(1, 10)); // 1..9
        else
            chars[0] = (char)('0' + RandomNumberGenerator.GetInt32(10));     // 0..9

        // بقیه ارقام
        for (int i = 1; i < digits; i++)
            chars[i] = (char)('0' + RandomNumberGenerator.GetInt32(10));

        return new string(chars);
    }
}
