// <auto-generated/>

#nullable disable

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

namespace OpenAI.Audio
{
    public partial class StreamingAudioTranscriptionTextDoneUpdate : IJsonModel<StreamingAudioTranscriptionTextDoneUpdate>
    {
        internal StreamingAudioTranscriptionTextDoneUpdate() : this(StreamingAudioTranscriptionUpdateKind.TranscriptTextDone, null, null, null)
        {
        }

        void IJsonModel<StreamingAudioTranscriptionTextDoneUpdate>.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<StreamingAudioTranscriptionTextDoneUpdate>)this).GetFormatFromOptions(options) : options.Format;
            if (format != "J")
            {
                throw new FormatException($"The model {nameof(StreamingAudioTranscriptionTextDoneUpdate)} does not support writing '{format}' format.");
            }
            base.JsonModelWriteCore(writer, options);
            if (_additionalBinaryDataProperties?.ContainsKey("text") != true)
            {
                writer.WritePropertyName("text"u8);
                writer.WriteStringValue(Text);
            }
            if (Optional.IsCollectionDefined(TranscriptionTokenLogProbabilities) && _additionalBinaryDataProperties?.ContainsKey("logprobs") != true)
            {
                writer.WritePropertyName("logprobs"u8);
                writer.WriteStartArray();
                foreach (AudioTokenLogProbabilityDetails item in TranscriptionTokenLogProbabilities)
                {
                    writer.WriteObjectValue(item, options);
                }
                writer.WriteEndArray();
            }
        }

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

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

        internal static StreamingAudioTranscriptionTextDoneUpdate DeserializeStreamingAudioTranscriptionTextDoneUpdate(JsonElement element, ModelReaderWriterOptions options)
        {
            if (element.ValueKind == JsonValueKind.Null)
            {
                return null;
            }
            StreamingAudioTranscriptionUpdateKind kind = default;
            IDictionary<string, BinaryData> additionalBinaryDataProperties = new ChangeTrackingDictionary<string, BinaryData>();
            string text = default;
            IReadOnlyList<AudioTokenLogProbabilityDetails> transcriptionTokenLogProbabilities = default;
            foreach (var prop in element.EnumerateObject())
            {
                if (prop.NameEquals("type"u8))
                {
                    kind = new StreamingAudioTranscriptionUpdateKind(prop.Value.GetString());
                    continue;
                }
                if (prop.NameEquals("text"u8))
                {
                    text = prop.Value.GetString();
                    continue;
                }
                if (prop.NameEquals("logprobs"u8))
                {
                    if (prop.Value.ValueKind == JsonValueKind.Null)
                    {
                        continue;
                    }
                    List<AudioTokenLogProbabilityDetails> array = new List<AudioTokenLogProbabilityDetails>();
                    foreach (var item in prop.Value.EnumerateArray())
                    {
                        array.Add(AudioTokenLogProbabilityDetails.DeserializeAudioTokenLogProbabilityDetails(item, options));
                    }
                    transcriptionTokenLogProbabilities = array;
                    continue;
                }
                // Plugin customization: remove options.Format != "W" check
                additionalBinaryDataProperties.Add(prop.Name, BinaryData.FromString(prop.Value.GetRawText()));
            }
            return new StreamingAudioTranscriptionTextDoneUpdate(kind, additionalBinaryDataProperties, text, transcriptionTokenLogProbabilities ?? new ChangeTrackingList<AudioTokenLogProbabilityDetails>());
        }

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

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

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

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

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