// <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 InternalEvalRunStoredCompletionsDataContentSource : IJsonModel<InternalEvalRunStoredCompletionsDataContentSource>
    {
        internal InternalEvalRunStoredCompletionsDataContentSource() : this(InternalEvalRunDataContentSourceType.StoredCompletions, null, null, null, default, default, default)
        {
        }

        void IJsonModel<InternalEvalRunStoredCompletionsDataContentSource>.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<InternalEvalRunStoredCompletionsDataContentSource>)this).GetFormatFromOptions(options) : options.Format;
            if (format != "J")
            {
                throw new FormatException($"The model {nameof(InternalEvalRunStoredCompletionsDataContentSource)} does not support writing '{format}' format.");
            }
            base.JsonModelWriteCore(writer, options);
            if (_additionalBinaryDataProperties?.ContainsKey("metadata") != true)
            {
                if (Optional.IsCollectionDefined(Metadata))
                {
                    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();
                }
                else
                {
                    writer.WriteNull("metadata"u8);
                }
            }
            if (Optional.IsDefined(Model) && _additionalBinaryDataProperties?.ContainsKey("model") != true)
            {
                writer.WritePropertyName("model"u8);
                writer.WriteStringValue(Model);
            }
            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(Limit) && _additionalBinaryDataProperties?.ContainsKey("limit") != true)
            {
                writer.WritePropertyName("limit"u8);
                writer.WriteNumberValue(Limit.Value);
            }
        }

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

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

        internal static InternalEvalRunStoredCompletionsDataContentSource DeserializeInternalEvalRunStoredCompletionsDataContentSource(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;
            int? createdAfter = default;
            int? createdBefore = default;
            int? limit = 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)
                    {
                        metadata = new ChangeTrackingDictionary<string, string>();
                        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("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("limit"u8))
                {
                    if (prop.Value.ValueKind == JsonValueKind.Null)
                    {
                        limit = null;
                        continue;
                    }
                    limit = prop.Value.GetInt32();
                    continue;
                }
                // Plugin customization: remove options.Format != "W" check
                additionalBinaryDataProperties.Add(prop.Name, BinaryData.FromString(prop.Value.GetRawText()));
            }
            return new InternalEvalRunStoredCompletionsDataContentSource(
                kind,
                additionalBinaryDataProperties,
                metadata,
                model,
                createdAfter,
                createdBefore,
                limit);
        }

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

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

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

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

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