// <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 InternalEvalItemContentInputText : IJsonModel<InternalEvalItemContentInputText>
    {
        internal InternalEvalItemContentInputText() : this(InternalEvalItemContentType.InputText, null, null)
        {
        }

        void IJsonModel<InternalEvalItemContentInputText>.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<InternalEvalItemContentInputText>)this).GetFormatFromOptions(options) : options.Format;
            if (format != "J")
            {
                throw new FormatException($"The model {nameof(InternalEvalItemContentInputText)} does not support writing '{format}' format.");
            }
            base.JsonModelWriteCore(writer, options);
            if (_additionalBinaryDataProperties?.ContainsKey("text") != true)
            {
                writer.WritePropertyName("text"u8);
                writer.WriteStringValue(Text);
            }
        }

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

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

        internal static InternalEvalItemContentInputText DeserializeInternalEvalItemContentInputText(JsonElement element, ModelReaderWriterOptions options)
        {
            if (element.ValueKind == JsonValueKind.Null)
            {
                return null;
            }
            InternalEvalItemContentType kind = default;
            IDictionary<string, BinaryData> additionalBinaryDataProperties = new ChangeTrackingDictionary<string, BinaryData>();
            string text = default;
            foreach (var prop in element.EnumerateObject())
            {
                if (prop.NameEquals("type"u8))
                {
                    kind = new InternalEvalItemContentType(prop.Value.GetString());
                    continue;
                }
                if (prop.NameEquals("text"u8))
                {
                    text = prop.Value.GetString();
                    continue;
                }
                // Plugin customization: remove options.Format != "W" check
                additionalBinaryDataProperties.Add(prop.Name, BinaryData.FromString(prop.Value.GetRawText()));
            }
            return new InternalEvalItemContentInputText(kind, additionalBinaryDataProperties, text);
        }

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

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

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

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

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