namespace StellaOps.Feedser.Merge.Services;

using System.Security.Cryptography;
using System.Text;
using StellaOps.Feedser.Models;

/// <summary>
/// Computes deterministic hashes over canonical advisory JSON payloads.
/// </summary>
public sealed class CanonicalHashCalculator
{
    private static readonly UTF8Encoding Utf8NoBom = new(false);

    public byte[] ComputeHash(Advisory? advisory)
    {
        if (advisory is null)
        {
            return Array.Empty<byte>();
        }

        var canonical = CanonicalJsonSerializer.Serialize(CanonicalJsonSerializer.Normalize(advisory));
        var payload = Utf8NoBom.GetBytes(canonical);
        return SHA256.HashData(payload);
    }
}
