using Microsoft.Extensions.Caching.Memory;

namespace JobsMedical.Web.Services;

/// <summary>
/// One-time-code issuing/verification. Codes live in memory for 5 minutes. In dev the code is
/// returned to the caller so it can be shown on screen; in production this is where an Iranian
/// SMS gateway (Kavenegar / SMS.ir) would send the code instead.
/// </summary>
public class OtpService
{
    private readonly IMemoryCache _cache;
    public OtpService(IMemoryCache cache) => _cache = cache;

    private static string Key(string phone) => $"otp:{Normalize(phone)}";

    /// <summary>Generate, store, and (in dev) return a 5-digit code for the phone.</summary>
    public string Issue(string phone)
    {
        var code = Random.Shared.Next(10000, 100000).ToString();
        _cache.Set(Key(phone), code, TimeSpan.FromMinutes(5));
        // TODO(prod): send `code` via Kavenegar/SMS.ir instead of returning it.
        return code;
    }

    public bool Verify(string phone, string code)
    {
        if (_cache.TryGetValue(Key(phone), out string? stored) && stored == code?.Trim())
        {
            _cache.Remove(Key(phone));
            return true;
        }
        return false;
    }

    /// <summary>Normalize Iranian mobile numbers (Persian digits → Latin, strip spaces).</summary>
    public static string Normalize(string phone)
    {
        var chars = phone.Trim().ToCharArray();
        for (var i = 0; i < chars.Length; i++)
            if (chars[i] >= '۰' && chars[i] <= '۹') chars[i] = (char)('0' + (chars[i] - '۰'));
        return new string(chars).Replace(" ", "").Replace("-", "");
    }
}
