using System.Security.Cryptography; using System.Text; namespace Admin.NET.Plugin.AiDOP.DataPlatform.Core; public sealed class PurchaseOrderSchemaRegistry : ISchemaRegistry { public const string DatasetKey = "lab.erp.purchase-order.v1"; public const string SourceVersion = "po-source-v1"; private static readonly IngestContract PurchaseOrder = new( DatasetKey, "PURCHASE_ORDER", ReadSchema(SourceVersion, "po-source-v1.schema.json"), ReadSchema("raw-envelope-v1", "raw-envelope-v1.schema.json"), ReadSchema("MDP-PO-001@1.0.0", "mdp-po-001-v1.schema.json"), "po-map-v1", "po-rule-v1", "po-config-v1"); public IngestContract? Find(string datasetKey, string sourceSchemaVersion) => datasetKey == DatasetKey && sourceSchemaVersion == SourceVersion ? PurchaseOrder : null; private static SchemaDefinition ReadSchema(string version, string file) { var type = typeof(PurchaseOrderSchemaRegistry); using var resource = type.Assembly.GetManifestResourceStream(type, $"Schemas.{file}") ?? throw new InvalidOperationException($"Missing ingress schema resource {file}."); using var output = new MemoryStream(); resource.CopyTo(output); var bytes = output.ToArray(); return new SchemaDefinition(new SchemaStamp(version, ComputeSha256(bytes)), Encoding.UTF8.GetString(bytes)); } internal static string ComputeSha256(ReadOnlySpan bytes) => Convert.ToHexString(SHA256.HashData(bytes)).ToLowerInvariant(); }