using System.Text.RegularExpressions;

namespace JobsMedical.Web.Services;

/// <summary>
/// Pretty-URL slugs for SEO landing pages (e.g. /استخدام/پزشک-عمومی/تهران). We keep Persian
/// characters — Google indexes UTF-8 URLs fine and they read naturally — and just turn spaces into
/// hyphens. Matching is tolerant of ي/ك, ZWNJ and hyphen/space variants so a hand-typed or
/// search-engine-rewritten slug still resolves.
/// </summary>
public static class SeoSlug
{
    /// <summary>The canonical slug for a role/city name («پزشک عمومی» → «پزشک-عمومی»).</summary>
    public static string Of(string? name) => Key(name);

    /// <summary>True when <paramref name="slug"/> (from the URL) refers to <paramref name="name"/>.</summary>
    public static bool Matches(string? name, string? slug) => Key(name) == Key(slug);

    private static string Key(string? s) => Regex.Replace(
        (s ?? "").Trim().Replace('ي', 'ی').Replace('ك', 'ک').Replace('‌', ' ').Replace('-', ' '),
        @"\s+", "-").ToLowerInvariant();
}
