// <auto-generated/>

#nullable disable

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

namespace OpenAI.Evals
{
    internal partial class InternalEvalRunOutputItemSample : IJsonModel<InternalEvalRunOutputItemSample>
    {
        internal InternalEvalRunOutputItemSample() : this(null, null, null, null, null, null, default, default, default, default, null)
        {
        }

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

        protected virtual void JsonModelWriteCore(Utf8JsonWriter writer, ModelReaderWriterOptions options)
        {
            string format = options.Format == "W" ? ((IPersistableModel<InternalEvalRunOutputItemSample>)this).GetFormatFromOptions(options) : options.Format;
            if (format != "J")
            {
                throw new FormatException($"The model {nameof(InternalEvalRunOutputItemSample)} does not support writing '{format}' format.");
            }
            if (_additionalBinaryDataProperties?.ContainsKey("input") != true)
            {
                writer.WritePropertyName("input"u8);
                writer.WriteStartArray();
                foreach (InternalEvalRunOutputItemSampleInput item in Input)
                {
                    writer.WriteObjectValue(item, options);
                }
                writer.WriteEndArray();
            }
            if (_additionalBinaryDataProperties?.ContainsKey("output") != true)
            {
                writer.WritePropertyName("output"u8);
                writer.WriteStartArray();
                foreach (InternalEvalRunOutputItemSampleOutput item in Output)
                {
                    writer.WriteObjectValue(item, options);
                }
                writer.WriteEndArray();
            }
            if (_additionalBinaryDataProperties?.ContainsKey("finish_reason") != true)
            {
                writer.WritePropertyName("finish_reason"u8);
                writer.WriteStringValue(FinishReason);
            }
            if (_additionalBinaryDataProperties?.ContainsKey("model") != true)
            {
                writer.WritePropertyName("model"u8);
                writer.WriteStringValue(Model);
            }
            if (_additionalBinaryDataProperties?.ContainsKey("usage") != true)
            {
                writer.WritePropertyName("usage"u8);
                writer.WriteObjectValue(Usage, options);
            }
            if (_additionalBinaryDataProperties?.ContainsKey("error") != true)
            {
                writer.WritePropertyName("error"u8);
                writer.WriteObjectValue(Error, options);
            }
            if (_additionalBinaryDataProperties?.ContainsKey("temperature") != true)
            {
                writer.WritePropertyName("temperature"u8);
                writer.WriteNumberValue(Temperature);
            }
            if (_additionalBinaryDataProperties?.ContainsKey("max_completion_tokens") != true)
            {
                writer.WritePropertyName("max_completion_tokens"u8);
                writer.WriteNumberValue(MaxCompletionTokens);
            }
            if (_additionalBinaryDataProperties?.ContainsKey("top_p") != true)
            {
                writer.WritePropertyName("top_p"u8);
                writer.WriteNumberValue(TopP);
            }
            if (_additionalBinaryDataProperties?.ContainsKey("seed") != true)
            {
                writer.WritePropertyName("seed"u8);
                writer.WriteNumberValue(Seed);
            }
            // Plugin customization: remove options.Format != "W" check
            if (_additionalBinaryDataProperties != null)
            {
                foreach (var item in _additionalBinaryDataProperties)
                {
                    if (ModelSerializationExtensions.IsSentinelValue(item.Value))
                    {
                        continue;
                    }
                    writer.WritePropertyName(item.Key);
#if NET6_0_OR_GREATER
                    writer.WriteRawValue(item.Value);
#else
                    using (JsonDocument document = JsonDocument.Parse(item.Value))
                    {
                        JsonSerializer.Serialize(writer, document.RootElement);
                    }
#endif
                }
            }
        }

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

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

        internal static InternalEvalRunOutputItemSample DeserializeInternalEvalRunOutputItemSample(JsonElement element, ModelReaderWriterOptions options)
        {
            if (element.ValueKind == JsonValueKind.Null)
            {
                return null;
            }
            IList<InternalEvalRunOutputItemSampleInput> input = default;
            IList<InternalEvalRunOutputItemSampleOutput> output = default;
            string finishReason = default;
            string model = default;
            InternalEvalRunOutputItemSampleUsage usage = default;
            InternalEvalApiError error = default;
            float temperature = default;
            int maxCompletionTokens = default;
            float topP = default;
            int seed = default;
            IDictionary<string, BinaryData> additionalBinaryDataProperties = new ChangeTrackingDictionary<string, BinaryData>();
            foreach (var prop in element.EnumerateObject())
            {
                if (prop.NameEquals("input"u8))
                {
                    List<InternalEvalRunOutputItemSampleInput> array = new List<InternalEvalRunOutputItemSampleInput>();
                    foreach (var item in prop.Value.EnumerateArray())
                    {
                        array.Add(InternalEvalRunOutputItemSampleInput.DeserializeInternalEvalRunOutputItemSampleInput(item, options));
                    }
                    input = array;
                    continue;
                }
                if (prop.NameEquals("output"u8))
                {
                    List<InternalEvalRunOutputItemSampleOutput> array = new List<InternalEvalRunOutputItemSampleOutput>();
                    foreach (var item in prop.Value.EnumerateArray())
                    {
                        array.Add(InternalEvalRunOutputItemSampleOutput.DeserializeInternalEvalRunOutputItemSampleOutput(item, options));
                    }
                    output = array;
                    continue;
                }
                if (prop.NameEquals("finish_reason"u8))
                {
                    finishReason = prop.Value.GetString();
                    continue;
                }
                if (prop.NameEquals("model"u8))
                {
                    model = prop.Value.GetString();
                    continue;
                }
                if (prop.NameEquals("usage"u8))
                {
                    usage = InternalEvalRunOutputItemSampleUsage.DeserializeInternalEvalRunOutputItemSampleUsage(prop.Value, options);
                    continue;
                }
                if (prop.NameEquals("error"u8))
                {
                    error = InternalEvalApiError.DeserializeInternalEvalApiError(prop.Value, options);
                    continue;
                }
                if (prop.NameEquals("temperature"u8))
                {
                    temperature = prop.Value.GetSingle();
                    continue;
                }
                if (prop.NameEquals("max_completion_tokens"u8))
                {
                    maxCompletionTokens = prop.Value.GetInt32();
                    continue;
                }
                if (prop.NameEquals("top_p"u8))
                {
                    topP = prop.Value.GetSingle();
                    continue;
                }
                if (prop.NameEquals("seed"u8))
                {
                    seed = prop.Value.GetInt32();
                    continue;
                }
                // Plugin customization: remove options.Format != "W" check
                additionalBinaryDataProperties.Add(prop.Name, BinaryData.FromString(prop.Value.GetRawText()));
            }
            return new InternalEvalRunOutputItemSample(
                input,
                output,
                finishReason,
                model,
                usage,
                error,
                temperature,
                maxCompletionTokens,
                topP,
                seed,
                additionalBinaryDataProperties);
        }

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

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

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

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

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