#nullable enable
using System.Text.RegularExpressions;
using Baya.SharedKernel.Extensions;

namespace Baya.Application.Features.Identity;

/// <summary>
/// Normalizes and validates an Iranian IBAN (شبا): the canonical <c>IR</c> + 24 digits, uppercase, no
/// spaces (Persian digits translated). The canonical form is what gets stored, encrypted, and hashed —
/// so the deterministic <c>iban_hash</c> uniqueness holds regardless of how the nurse typed it.
/// </summary>
internal static partial class Sheba
{
    [GeneratedRegex(@"^IR\d{24}$")]
    private static partial Regex ShebaPattern();

    public static string? Normalize(string? raw)
    {
        if (string.IsNullOrWhiteSpace(raw))
            return null;

        var candidate = raw.Trim().Fa2En().Replace(" ", string.Empty).Replace("-", string.Empty).ToUpperInvariant();
        return ShebaPattern().IsMatch(candidate) ? candidate : null;
    }

    public static bool IsValid(string? raw) => Normalize(raw) is not null;
}
