using System.Security.Cryptography;
using System.Text;
using Kbx.Shared.Configuration.Generated;
using Microsoft.Extensions.Configuration;

namespace Kbx.Shared.Configuration;

public static class KbxConfigurationFingerprint
{
    public static string Compute(IConfiguration configuration)
    {
        var lines = KbxConfigurationCatalog.Settings.Values
            .Where(x => !x.Secret)
            .OrderBy(x => x.Key, StringComparer.Ordinal)
            .Select(x => $"{x.Key}={configuration[x.Key] ?? x.DefaultValue ?? string.Empty}");

        var bytes = SHA256.HashData(Encoding.UTF8.GetBytes(string.Join("\n", lines)));
        return Convert.ToHexString(bytes).ToLowerInvariant();
    }
}
