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

namespace StellaOps.Feedser.Source.Vndr.Chromium.Internal;

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

    public static JsonSchema Schema => Cached.Value;

    private static JsonSchema Load()
    {
        var assembly = typeof(ChromiumSchemaProvider).GetTypeInfo().Assembly;
        const string resourceName = "StellaOps.Feedser.Source.Vndr.Chromium.Schemas.chromium-post.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);
    }
}
