// <auto-generated/>

#nullable disable

using System;
using System.ClientModel.Primitives;
using System.Collections.Generic;
using System.Text.Json;
using OpenAI;
using OpenAI.Evals;

namespace OpenAI.Graders
{
    public partial class GraderLabelModel : IJsonModel<GraderLabelModel>
    {
        internal GraderLabelModel() : this(GraderType.LabelModel, null, null, null, null, null, null)
        {
        }

        void IJsonModel<GraderLabelModel>.Write(Utf8JsonWriter writer, ModelReaderWriterOptions options)
        {
            writer.WriteStartObject();
            JsonModelWriteCore(writer, options);
            writer.WriteEndObject();
        }

        protected override void JsonModelWriteCore(Utf8JsonWriter writer, ModelReaderWriterOptions options)
        {
            string format = options.Format == "W" ? ((IPersistableModel<GraderLabelModel>)this).GetFormatFromOptions(options) : options.Format;
            if (format != "J")
            {
                throw new FormatException($"The model {nameof(GraderLabelModel)} does not support writing '{format}' format.");
            }
            base.JsonModelWriteCore(writer, options);
            if (_additionalBinaryDataProperties?.ContainsKey("name") != true)
            {
                writer.WritePropertyName("name"u8);
                writer.WriteStringValue(Name);
            }
            if (_additionalBinaryDataProperties?.ContainsKey("model") != true)
            {
                writer.WritePropertyName("model"u8);
                writer.WriteStringValue(Model);
            }
            if (_additionalBinaryDataProperties?.ContainsKey("input") != true)
            {
                writer.WritePropertyName("input"u8);
                writer.WriteStartArray();
                foreach (InternalEvalItem item in Input)
                {
                    writer.WriteObjectValue(item, options);
                }
                writer.WriteEndArray();
            }
            if (_additionalBinaryDataProperties?.ContainsKey("labels") != true)
            {
                writer.WritePropertyName("labels"u8);
                writer.WriteStartArray();
                foreach (string item in Labels)
                {
                    if (item == null)
                    {
                        writer.WriteNullValue();
                        continue;
                    }
                    writer.WriteStringValue(item);
                }
                writer.WriteEndArray();
            }
            if (_additionalBinaryDataProperties?.ContainsKey("passing_labels") != true)
            {
                writer.WritePropertyName("passing_labels"u8);
                writer.WriteStartArray();
                foreach (string item in PassingLabels)
                {
                    if (item == null)
                    {
                        writer.WriteNullValue();
                        continue;
                    }
                    writer.WriteStringValue(item);
                }
                writer.WriteEndArray();
            }
        }

        GraderLabelModel IJsonModel<GraderLabelModel>.Create(ref Utf8JsonReader reader, ModelReaderWriterOptions options) => (GraderLabelModel)JsonModelCreateCore(ref reader, options);

        protected override Grader JsonModelCreateCore(ref Utf8JsonReader reader, ModelReaderWriterOptions options)
        {
            string format = options.Format == "W" ? ((IPersistableModel<GraderLabelModel>)this).GetFormatFromOptions(options) : options.Format;
            if (format != "J")
            {
                throw new FormatException($"The model {nameof(GraderLabelModel)} does not support reading '{format}' format.");
            }
            using JsonDocument document = JsonDocument.ParseValue(ref reader);
            return DeserializeGraderLabelModel(document.RootElement, options);
        }

        internal static GraderLabelModel DeserializeGraderLabelModel(JsonElement element, ModelReaderWriterOptions options)
        {
            if (element.ValueKind == JsonValueKind.Null)
            {
                return null;
            }
            GraderType kind = default;
            IDictionary<string, BinaryData> additionalBinaryDataProperties = new ChangeTrackingDictionary<string, BinaryData>();
            string name = default;
            string model = default;
            IList<InternalEvalItem> input = default;
            IList<string> labels = default;
            IList<string> passingLabels = default;
            foreach (var prop in element.EnumerateObject())
            {
                if (prop.NameEquals("type"u8))
                {
                    kind = new GraderType(prop.Value.GetString());
                    continue;
                }
                if (prop.NameEquals("name"u8))
                {
                    name = prop.Value.GetString();
                    continue;
                }
                if (prop.NameEquals("model"u8))
                {
                    model = prop.Value.GetString();
                    continue;
                }
                if (prop.NameEquals("input"u8))
                {
                    List<InternalEvalItem> array = new List<InternalEvalItem>();
                    foreach (var item in prop.Value.EnumerateArray())
                    {
                        array.Add(InternalEvalItem.DeserializeInternalEvalItem(item, options));
                    }
                    input = array;
                    continue;
                }
                if (prop.NameEquals("labels"u8))
                {
                    List<string> array = new List<string>();
                    foreach (var item in prop.Value.EnumerateArray())
                    {
                        if (item.ValueKind == JsonValueKind.Null)
                        {
                            array.Add(null);
                        }
                        else
                        {
                            array.Add(item.GetString());
                        }
                    }
                    labels = array;
                    continue;
                }
                if (prop.NameEquals("passing_labels"u8))
                {
                    List<string> array = new List<string>();
                    foreach (var item in prop.Value.EnumerateArray())
                    {
                        if (item.ValueKind == JsonValueKind.Null)
                        {
                            array.Add(null);
                        }
                        else
                        {
                            array.Add(item.GetString());
                        }
                    }
                    passingLabels = array;
                    continue;
                }
                // Plugin customization: remove options.Format != "W" check
                additionalBinaryDataProperties.Add(prop.Name, BinaryData.FromString(prop.Value.GetRawText()));
            }
            return new GraderLabelModel(
                kind,
                additionalBinaryDataProperties,
                name,
                model,
                input,
                labels,
                passingLabels);
        }

        BinaryData IPersistableModel<GraderLabelModel>.Write(ModelReaderWriterOptions options) => PersistableModelWriteCore(options);

        protected override BinaryData PersistableModelWriteCore(ModelReaderWriterOptions options)
        {
            string format = options.Format == "W" ? ((IPersistableModel<GraderLabelModel>)this).GetFormatFromOptions(options) : options.Format;
            switch (format)
            {
                case "J":
                    return ModelReaderWriter.Write(this, options, OpenAIContext.Default);
                default:
                    throw new FormatException($"The model {nameof(GraderLabelModel)} does not support writing '{options.Format}' format.");
            }
        }

        GraderLabelModel IPersistableModel<GraderLabelModel>.Create(BinaryData data, ModelReaderWriterOptions options) => (GraderLabelModel)PersistableModelCreateCore(data, options);

        protected override Grader PersistableModelCreateCore(BinaryData data, ModelReaderWriterOptions options)
        {
            string format = options.Format == "W" ? ((IPersistableModel<GraderLabelModel>)this).GetFormatFromOptions(options) : options.Format;
            switch (format)
            {
                case "J":
                    using (JsonDocument document = JsonDocument.Parse(data))
                    {
                        return DeserializeGraderLabelModel(document.RootElement, options);
                    }
                default:
                    throw new FormatException($"The model {nameof(GraderLabelModel)} does not support reading '{options.Format}' format.");
            }
        }

        string IPersistableModel<GraderLabelModel>.GetFormatFromOptions(ModelReaderWriterOptions options) => "J";
    }
}
