// <auto-generated/>

#nullable disable

using System;
using System.ClientModel.Primitives;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Text.Json;
using OpenAI;
using OpenAI.Graders;

namespace OpenAI.Evals
{
    internal partial class InternalEvalGraderLabelModelParams : IJsonModel<InternalEvalGraderLabelModelParams>
    {
        internal InternalEvalGraderLabelModelParams() : this(InternalGraderType.LabelModel, null, null, null, null, null, null)
        {
        }

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

        [Experimental("OPENAI001")]
        protected override void JsonModelWriteCore(Utf8JsonWriter writer, ModelReaderWriterOptions options)
        {
            string format = options.Format == "W" ? ((IPersistableModel<InternalEvalGraderLabelModelParams>)this).GetFormatFromOptions(options) : options.Format;
            if (format != "J")
            {
                throw new FormatException($"The model {nameof(InternalEvalGraderLabelModelParams)} 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 (BinaryData item in Input)
                {
                    if (item == null)
                    {
                        writer.WriteNullValue();
                        continue;
                    }
#if NET6_0_OR_GREATER
                    writer.WriteRawValue(item);
#else
                    using (JsonDocument document = JsonDocument.Parse(item))
                    {
                        JsonSerializer.Serialize(writer, document.RootElement);
                    }
#endif
                }
                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();
            }
        }

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

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

        internal static InternalEvalGraderLabelModelParams DeserializeInternalEvalGraderLabelModelParams(JsonElement element, ModelReaderWriterOptions options)
        {
            if (element.ValueKind == JsonValueKind.Null)
            {
                return null;
            }
            InternalGraderType kind = default;
            IDictionary<string, BinaryData> additionalBinaryDataProperties = new ChangeTrackingDictionary<string, BinaryData>();
            string name = default;
            string model = default;
            IList<BinaryData> input = default;
            IList<string> labels = default;
            IList<string> passingLabels = default;
            foreach (var prop in element.EnumerateObject())
            {
                if (prop.NameEquals("type"u8))
                {
                    kind = new InternalGraderType(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<BinaryData> array = new List<BinaryData>();
                    foreach (var item in prop.Value.EnumerateArray())
                    {
                        if (item.ValueKind == JsonValueKind.Null)
                        {
                            array.Add(null);
                        }
                        else
                        {
                            array.Add(BinaryData.FromString(item.GetRawText()));
                        }
                    }
                    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 InternalEvalGraderLabelModelParams(
                kind,
                additionalBinaryDataProperties,
                name,
                model,
                input,
                labels,
                passingLabels);
        }

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

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

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

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

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