using System.IO;
using System.Reflection;
using System.Threading;
using Json.Schema;

namespace StellaOps.Feedser.Source.Nvd.Internal;

internal static class NvdSchemaProvider
{
    private static readonly Lazy<JsonSchema> Cached = new(LoadSchema, LazyThreadSafetyMode.ExecutionAndPublication);

    public static JsonSchema Schema => Cached.Value;

    private static JsonSchema LoadSchema()
    {
        var assembly = typeof(NvdSchemaProvider).GetTypeInfo().Assembly;
        const string resourceName = "StellaOps.Feedser.Source.Nvd.Schemas.nvd-vulnerability.schema.json";

        using var stream = assembly.GetManifestResourceStream(resourceName)
            ?? throw new InvalidOperationException($"Embedded schema '{resourceName}' not found.");
        using var reader = new StreamReader(stream);
        var schemaText = reader.ReadToEnd();
        return JsonSchema.FromText(schemaText);
    }
}
