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

namespace NexusReader.Infrastructure.Helpers;

public static class ContentHasher
{
    public static string ComputeHash(string input)
    {
        if (string.IsNullOrWhiteSpace(input))
        {
            return string.Empty;
        }

        var normalizedInput = Normalize(input);
        
        var inputBytes = Encoding.UTF8.GetBytes(normalizedInput);
        var hashBytes = SHA256.HashData(inputBytes);
        
        return Convert.ToHexString(hashBytes).ToLowerInvariant();
    }

    public static string Normalize(string input)
    {
        if (string.IsNullOrWhiteSpace(input))
        {
            return string.Empty;
        }

        // Trim and collapse all consecutive whitespace characters into a single space
        var normalized = Regex.Replace(input.Trim(), @"\s+", " ");
        
        // Convert to lower-case as AI analysis is generally not case-sensitive for concepts/quizzes
        return normalized.ToLowerInvariant();
    }
}
