using System;
using System.IO;
using System.Reflection;
using System.Text;

namespace StellaOps.Policy;

public static class PolicySchemaResource
{
    private const string SchemaResourceName = "StellaOps.Policy.Schemas.policy-schema@1.json";

    public static Stream OpenSchemaStream()
    {
        var assembly = Assembly.GetExecutingAssembly();
        var stream = assembly.GetManifestResourceStream(SchemaResourceName);
        if (stream is null)
        {
            throw new InvalidOperationException($"Unable to locate embedded schema resource '{SchemaResourceName}'.");
        }

        return stream;
    }

    public static string ReadSchemaJson()
    {
        using var stream = OpenSchemaStream();
        using var reader = new StreamReader(stream, Encoding.UTF8, detectEncodingFromByteOrderMarks: true);
        return reader.ReadToEnd();
    }
}
