// <auto-generated/>

#nullable disable

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

namespace OpenAI.Evals
{
    internal partial class InternalEvalRunResponsesDataContentSource : IJsonModel<InternalEvalRunResponsesDataContentSource>
    {
        void IJsonModel<InternalEvalRunResponsesDataContentSource>.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<InternalEvalRunResponsesDataContentSource>)this).GetFormatFromOptions(options) : options.Format;
            if (format != "J")
            {
                throw new FormatException($"The model {nameof(InternalEvalRunResponsesDataContentSource)} does not support writing '{format}' format.");
            }
            base.JsonModelWriteCore(writer, options);
            if (Optional.IsCollectionDefined(Metadata) && _additionalBinaryDataProperties?.ContainsKey("metadata") != true)
            {
                writer.WritePropertyName("metadata"u8);
                writer.WriteStartObject();
                foreach (var item in Metadata)
                {
                    writer.WritePropertyName(item.Key);
                    if (item.Value == null)
                    {
                        writer.WriteNullValue();
                        continue;
                    }
                    writer.WriteStringValue(item.Value);
                }
                writer.WriteEndObject();
            }
            if (Optional.IsDefined(Model) && _additionalBinaryDataProperties?.ContainsKey("model") != true)
            {
                writer.WritePropertyName("model"u8);
                writer.WriteStringValue(Model);
            }
            if (Optional.IsDefined(InstructionsSearch) && _additionalBinaryDataProperties?.ContainsKey("instructions_search") != true)
            {
                writer.WritePropertyName("instructions_search"u8);
                writer.WriteStringValue(InstructionsSearch);
            }
            if (Optional.IsDefined(CreatedAfter) && _additionalBinaryDataProperties?.ContainsKey("created_after") != true)
            {
                writer.WritePropertyName("created_after"u8);
                writer.WriteNumberValue(CreatedAfter.Value);
            }
            if (Optional.IsDefined(CreatedBefore) && _additionalBinaryDataProperties?.ContainsKey("created_before") != true)
            {
                writer.WritePropertyName("created_before"u8);
                writer.WriteNumberValue(CreatedBefore.Value);
            }
            if (Optional.IsDefined(ReasoningEffort) && _additionalBinaryDataProperties?.ContainsKey("reasoning_effort") != true)
            {
                writer.WritePropertyName("reasoning_effort"u8);
                writer.WriteStringValue(ReasoningEffort.Value.ToString());
            }
            if (Optional.IsDefined(Temperature) && _additionalBinaryDataProperties?.ContainsKey("temperature") != true)
            {
                writer.WritePropertyName("temperature"u8);
                writer.WriteNumberValue(Temperature.Value);
            }
            if (Optional.IsDefined(TopP) && _additionalBinaryDataProperties?.ContainsKey("top_p") != true)
            {
                writer.WritePropertyName("top_p"u8);
                writer.WriteNumberValue(TopP.Value);
            }
            if (Optional.IsCollectionDefined(Users) && _additionalBinaryDataProperties?.ContainsKey("users") != true)
            {
                writer.WritePropertyName("users"u8);
                writer.WriteStartArray();
                foreach (string item in Users)
                {
                    if (item == null)
                    {
                        writer.WriteNullValue();
                        continue;
                    }
                    writer.WriteStringValue(item);
                }
                writer.WriteEndArray();
            }
            if (Optional.IsCollectionDefined(Tools) && _additionalBinaryDataProperties?.ContainsKey("tools") != true)
            {
                writer.WritePropertyName("tools"u8);
                writer.WriteStartArray();
                foreach (string item in Tools)
                {
                    if (item == null)
                    {
                        writer.WriteNullValue();
                        continue;
                    }
                    writer.WriteStringValue(item);
                }
                writer.WriteEndArray();
            }
        }

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

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

        internal static InternalEvalRunResponsesDataContentSource DeserializeInternalEvalRunResponsesDataContentSource(JsonElement element, ModelReaderWriterOptions options)
        {
            if (element.ValueKind == JsonValueKind.Null)
            {
                return null;
            }
            InternalEvalRunDataContentSourceType kind = default;
            IDictionary<string, BinaryData> additionalBinaryDataProperties = new ChangeTrackingDictionary<string, BinaryData>();
            IDictionary<string, string> metadata = default;
            string model = default;
            string instructionsSearch = default;
            int? createdAfter = default;
            int? createdBefore = default;
            ChatReasoningEffortLevel? reasoningEffort = default;
            float? temperature = default;
            float? topP = default;
            IList<string> users = default;
            IList<string> tools = default;
            foreach (var prop in element.EnumerateObject())
            {
                if (prop.NameEquals("type"u8))
                {
                    kind = new InternalEvalRunDataContentSourceType(prop.Value.GetString());
                    continue;
                }
                if (prop.NameEquals("metadata"u8))
                {
                    if (prop.Value.ValueKind == JsonValueKind.Null)
                    {
                        continue;
                    }
                    Dictionary<string, string> dictionary = new Dictionary<string, string>();
                    foreach (var prop0 in prop.Value.EnumerateObject())
                    {
                        if (prop0.Value.ValueKind == JsonValueKind.Null)
                        {
                            dictionary.Add(prop0.Name, null);
                        }
                        else
                        {
                            dictionary.Add(prop0.Name, prop0.Value.GetString());
                        }
                    }
                    metadata = dictionary;
                    continue;
                }
                if (prop.NameEquals("model"u8))
                {
                    if (prop.Value.ValueKind == JsonValueKind.Null)
                    {
                        model = null;
                        continue;
                    }
                    model = prop.Value.GetString();
                    continue;
                }
                if (prop.NameEquals("instructions_search"u8))
                {
                    if (prop.Value.ValueKind == JsonValueKind.Null)
                    {
                        instructionsSearch = null;
                        continue;
                    }
                    instructionsSearch = prop.Value.GetString();
                    continue;
                }
                if (prop.NameEquals("created_after"u8))
                {
                    if (prop.Value.ValueKind == JsonValueKind.Null)
                    {
                        createdAfter = null;
                        continue;
                    }
                    createdAfter = prop.Value.GetInt32();
                    continue;
                }
                if (prop.NameEquals("created_before"u8))
                {
                    if (prop.Value.ValueKind == JsonValueKind.Null)
                    {
                        createdBefore = null;
                        continue;
                    }
                    createdBefore = prop.Value.GetInt32();
                    continue;
                }
                if (prop.NameEquals("reasoning_effort"u8))
                {
                    if (prop.Value.ValueKind == JsonValueKind.Null)
                    {
                        reasoningEffort = null;
                        continue;
                    }
                    reasoningEffort = new ChatReasoningEffortLevel(prop.Value.GetString());
                    continue;
                }
                if (prop.NameEquals("temperature"u8))
                {
                    if (prop.Value.ValueKind == JsonValueKind.Null)
                    {
                        temperature = null;
                        continue;
                    }
                    temperature = prop.Value.GetSingle();
                    continue;
                }
                if (prop.NameEquals("top_p"u8))
                {
                    if (prop.Value.ValueKind == JsonValueKind.Null)
                    {
                        topP = null;
                        continue;
                    }
                    topP = prop.Value.GetSingle();
                    continue;
                }
                if (prop.NameEquals("users"u8))
                {
                    if (prop.Value.ValueKind == JsonValueKind.Null)
                    {
                        continue;
                    }
                    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());
                        }
                    }
                    users = array;
                    continue;
                }
                if (prop.NameEquals("tools"u8))
                {
                    if (prop.Value.ValueKind == JsonValueKind.Null)
                    {
                        continue;
                    }
                    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());
                        }
                    }
                    tools = array;
                    continue;
                }
                // Plugin customization: remove options.Format != "W" check
                additionalBinaryDataProperties.Add(prop.Name, BinaryData.FromString(prop.Value.GetRawText()));
            }
            return new InternalEvalRunResponsesDataContentSource(
                kind,
                additionalBinaryDataProperties,
                metadata ?? new ChangeTrackingDictionary<string, string>(),
                model,
                instructionsSearch,
                createdAfter,
                createdBefore,
                reasoningEffort,
                temperature,
                topP,
                users ?? new ChangeTrackingList<string>(),
                tools ?? new ChangeTrackingList<string>());
        }

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

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

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

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

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