using System.Text.RegularExpressions;
namespace HikvisionAttendanceService;
///
/// Single place for DeviceId trimming and session/fetch lookup keys so config, CLI, and logs stay aligned.
///
internal static class DeviceIdentity
{
private static readonly Regex FMNumericSuffix = new Regex(@"-FM(\d+)$", RegexOptions.IgnoreCase | RegexOptions.CultureInvariant);
/// Value stored from JSON after load — trim only; never replace user’s chosen id.
public static string NormalizeConfigured(string? raw)
{
if (string.IsNullOrWhiteSpace(raw))
return "";
return raw.Trim();
}
///
/// Stable key for FindSession / fetch: same physical device if only the "-FM" serial prefix differs
/// (e.g. DS-K1T642MFW-FM7378360 vs DS-K1T642MFW-7378360).
///
public static string CanonicalLookupKey(string? raw)
{
var s = NormalizeConfigured(raw);
if (s.Length == 0)
return "";
s = FMNumericSuffix.Replace(s, "-$1");
return s.ToUpperInvariant();
}
}