using System;
using System.Security.Cryptography;
using System.Text;

namespace Jellyfin.Plugin.SRFPlay.Utilities;

/// <summary>
/// Helper class for URN-related operations.
/// </summary>
public static class UrnHelper
{
    /// <summary>
    /// Generates a deterministic GUID from a URN.
    /// This ensures the same URN always produces the same GUID.
    /// MD5 is used for non-cryptographic purposes only (generating stable IDs).
    /// </summary>
    /// <param name="urn">The URN to convert.</param>
    /// <returns>A deterministic GUID string.</returns>
#pragma warning disable CA5351 // MD5 is used for non-cryptographic purposes (ID generation)
    public static string ToGuid(string urn)
    {
        var hash = MD5.HashData(Encoding.UTF8.GetBytes(urn));
        var guid = new Guid(hash);
        return guid.ToString();
    }
#pragma warning restore CA5351

    /// <summary>
    /// Determines whether a URN refers to a livestream (scheduled or continuous).
    /// This is a URN-only heuristic that needs no API call, so it stays usable long after
    /// the broadcast has ended and the media composition is no longer worth fetching.
    /// </summary>
    /// <param name="urn">The URN to inspect. May be null or empty.</param>
    /// <returns>True if the URN identifies livestream content.</returns>
    public static bool IsLivestreamUrn(string? urn)
    {
        return !string.IsNullOrEmpty(urn)
            && urn.Contains("livestream", StringComparison.OrdinalIgnoreCase);
    }
}
