// <auto-generated/>

#nullable disable

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

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

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

        [Experimental("OPENAI001")]
        protected virtual void JsonModelWriteCore(Utf8JsonWriter writer, ModelReaderWriterOptions options)
        {
            string format = options.Format == "W" ? ((IPersistableModel<InternalEvalRunOutputItem>)this).GetFormatFromOptions(options) : options.Format;
            if (format != "J")
            {
                throw new FormatException($"The model {nameof(InternalEvalRunOutputItem)} does not support writing '{format}' format.");
            }
            if (_additionalBinaryDataProperties?.ContainsKey("object") != true)
            {
                writer.WritePropertyName("object"u8);
                writer.WriteStringValue(Object);
            }
            if (_additionalBinaryDataProperties?.ContainsKey("id") != true)
            {
                writer.WritePropertyName("id"u8);
                writer.WriteStringValue(Id);
            }
            if (_additionalBinaryDataProperties?.ContainsKey("run_id") != true)
            {
                writer.WritePropertyName("run_id"u8);
                writer.WriteStringValue(RunId);
            }
            if (_additionalBinaryDataProperties?.ContainsKey("eval_id") != true)
            {
                writer.WritePropertyName("eval_id"u8);
                writer.WriteStringValue(EvalId);
            }
            if (_additionalBinaryDataProperties?.ContainsKey("created_at") != true)
            {
                writer.WritePropertyName("created_at"u8);
                writer.WriteNumberValue(CreatedAt, "U");
            }
            if (_additionalBinaryDataProperties?.ContainsKey("status") != true)
            {
                writer.WritePropertyName("status"u8);
                writer.WriteStringValue(Status);
            }
            if (_additionalBinaryDataProperties?.ContainsKey("datasource_item_id") != true)
            {
                writer.WritePropertyName("datasource_item_id"u8);
                writer.WriteNumberValue(DatasourceItemId);
            }
            if (_additionalBinaryDataProperties?.ContainsKey("datasource_item") != true)
            {
                writer.WritePropertyName("datasource_item"u8);
                writer.WriteStartObject();
                foreach (var item in DatasourceItem)
                {
                    writer.WritePropertyName(item.Key);
                    if (item.Value == null)
                    {
                        writer.WriteNullValue();
                        continue;
                    }
#if NET6_0_OR_GREATER
                    writer.WriteRawValue(item.Value);
#else
                    using (JsonDocument document = JsonDocument.Parse(item.Value))
                    {
                        JsonSerializer.Serialize(writer, document.RootElement);
                    }
#endif
                }
                writer.WriteEndObject();
            }
            if (_additionalBinaryDataProperties?.ContainsKey("results") != true)
            {
                writer.WritePropertyName("results"u8);
                writer.WriteStartArray();
                foreach (IDictionary<string, BinaryData> item in Results)
                {
                    if (item == null)
                    {
                        writer.WriteNullValue();
                        continue;
                    }
                    writer.WriteStartObject();
                    foreach (var item0 in item)
                    {
                        writer.WritePropertyName(item0.Key);
                        if (item0.Value == null)
                        {
                            writer.WriteNullValue();
                            continue;
                        }
#if NET6_0_OR_GREATER
                        writer.WriteRawValue(item0.Value);
#else
                        using (JsonDocument document = JsonDocument.Parse(item0.Value))
                        {
                            JsonSerializer.Serialize(writer, document.RootElement);
                        }
#endif
                    }
                    writer.WriteEndObject();
                }
                writer.WriteEndArray();
            }
            if (_additionalBinaryDataProperties?.ContainsKey("sample") != true)
            {
                writer.WritePropertyName("sample"u8);
                writer.WriteObjectValue(Sample, options);
            }
            // 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
                }
            }
        }

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

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

        internal static InternalEvalRunOutputItem DeserializeInternalEvalRunOutputItem(JsonElement element, ModelReaderWriterOptions options)
        {
            if (element.ValueKind == JsonValueKind.Null)
            {
                return null;
            }
            string @object = default;
            string id = default;
            string runId = default;
            string evalId = default;
            DateTimeOffset createdAt = default;
            string status = default;
            int datasourceItemId = default;
            IDictionary<string, BinaryData> datasourceItem = default;
            IList<IDictionary<string, BinaryData>> results = default;
            InternalEvalRunOutputItemSample sample = default;
            IDictionary<string, BinaryData> additionalBinaryDataProperties = new ChangeTrackingDictionary<string, BinaryData>();
            foreach (var prop in element.EnumerateObject())
            {
                if (prop.NameEquals("object"u8))
                {
                    @object = prop.Value.GetString();
                    continue;
                }
                if (prop.NameEquals("id"u8))
                {
                    id = prop.Value.GetString();
                    continue;
                }
                if (prop.NameEquals("run_id"u8))
                {
                    runId = prop.Value.GetString();
                    continue;
                }
                if (prop.NameEquals("eval_id"u8))
                {
                    evalId = prop.Value.GetString();
                    continue;
                }
                if (prop.NameEquals("created_at"u8))
                {
                    createdAt = DateTimeOffset.FromUnixTimeSeconds(prop.Value.GetInt64());
                    continue;
                }
                if (prop.NameEquals("status"u8))
                {
                    status = prop.Value.GetString();
                    continue;
                }
                if (prop.NameEquals("datasource_item_id"u8))
                {
                    datasourceItemId = prop.Value.GetInt32();
                    continue;
                }
                if (prop.NameEquals("datasource_item"u8))
                {
                    Dictionary<string, BinaryData> dictionary = new Dictionary<string, BinaryData>();
                    foreach (var prop0 in prop.Value.EnumerateObject())
                    {
                        if (prop0.Value.ValueKind == JsonValueKind.Null)
                        {
                            dictionary.Add(prop0.Name, null);
                        }
                        else
                        {
                            dictionary.Add(prop0.Name, BinaryData.FromString(prop0.Value.GetRawText()));
                        }
                    }
                    datasourceItem = dictionary;
                    continue;
                }
                if (prop.NameEquals("results"u8))
                {
                    List<IDictionary<string, BinaryData>> array = new List<IDictionary<string, BinaryData>>();
                    foreach (var item in prop.Value.EnumerateArray())
                    {
                        if (item.ValueKind == JsonValueKind.Null)
                        {
                            array.Add(null);
                        }
                        else
                        {
                            Dictionary<string, BinaryData> dictionary = new Dictionary<string, BinaryData>();
                            foreach (var prop0 in item.EnumerateObject())
                            {
                                if (prop0.Value.ValueKind == JsonValueKind.Null)
                                {
                                    dictionary.Add(prop0.Name, null);
                                }
                                else
                                {
                                    dictionary.Add(prop0.Name, BinaryData.FromString(prop0.Value.GetRawText()));
                                }
                            }
                            array.Add(dictionary);
                        }
                    }
                    results = array;
                    continue;
                }
                if (prop.NameEquals("sample"u8))
                {
                    sample = InternalEvalRunOutputItemSample.DeserializeInternalEvalRunOutputItemSample(prop.Value, options);
                    continue;
                }
                // Plugin customization: remove options.Format != "W" check
                additionalBinaryDataProperties.Add(prop.Name, BinaryData.FromString(prop.Value.GetRawText()));
            }
            return new InternalEvalRunOutputItem(
                @object,
                id,
                runId,
                evalId,
                createdAt,
                status,
                datasourceItemId,
                datasourceItem,
                results,
                sample,
                additionalBinaryDataProperties);
        }

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

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

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

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

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