using System.Globalization;
using System.Text.RegularExpressions;
namespace SQLInsight.Hub.Support;
/// Case numbers SUP-000001, SUP-000002 … running per customer.
public static partial class CaseNumbers
{
public const string Prefix = "SUP-";
///
/// The number after the highest SUP-<digits> among . Other references (typed by hand,
/// e.g. from an older system) are ignored, so they never block or reset the sequence.
///
public static string Next(IEnumerable existing)
{
ArgumentNullException.ThrowIfNull(existing);
long highest = 0;
foreach (var reference in existing)
{
var match = Number().Match(reference ?? string.Empty);
if (match.Success && long.TryParse(match.Groups[1].Value, NumberStyles.None, CultureInfo.InvariantCulture, out var n) && n > highest)
{
highest = n;
}
}
return Format(highest + 1);
}
public static string Format(long number) => Prefix + number.ToString("D6", CultureInfo.InvariantCulture);
[GeneratedRegex(@"^\s*SUP-(\d{1,18})\s*$", RegexOptions.IgnoreCase | RegexOptions.CultureInvariant)]
private static partial Regex Number();
}