// <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.Realtime
{
    internal partial class InternalRealtimeTranscriptionSessionCreateResponse : IJsonModel<InternalRealtimeTranscriptionSessionCreateResponse>
    {
        internal InternalRealtimeTranscriptionSessionCreateResponse() : this(null, null, default, null, null, null)
        {
        }

        void IJsonModel<InternalRealtimeTranscriptionSessionCreateResponse>.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<InternalRealtimeTranscriptionSessionCreateResponse>)this).GetFormatFromOptions(options) : options.Format;
            if (format != "J")
            {
                throw new FormatException($"The model {nameof(InternalRealtimeTranscriptionSessionCreateResponse)} does not support writing '{format}' format.");
            }
            if (_additionalBinaryDataProperties?.ContainsKey("client_secret") != true)
            {
                writer.WritePropertyName("client_secret"u8);
                writer.WriteObjectValue(ClientSecret, options);
            }
            if (Optional.IsCollectionDefined(Modalities) && _additionalBinaryDataProperties?.ContainsKey("modalities") != true)
            {
                writer.WritePropertyName("modalities"u8);
                writer.WriteStartArray();
                foreach (InternalRealtimeRequestSessionModality item in Modalities)
                {
                    writer.WriteStringValue(item.ToString());
                }
                writer.WriteEndArray();
            }
            if (_additionalBinaryDataProperties?.ContainsKey("input_audio_format") != true)
            {
                writer.WritePropertyName("input_audio_format"u8);
                SerializeInputAudioFormatValue(writer, options);
            }
            if (Optional.IsDefined(InputAudioTranscription) && _additionalBinaryDataProperties?.ContainsKey("input_audio_transcription") != true)
            {
                writer.WritePropertyName("input_audio_transcription"u8);
                writer.WriteObjectValue(InputAudioTranscription, options);
            }
            if (Optional.IsDefined(TurnDetection) && _additionalBinaryDataProperties?.ContainsKey("turn_detection") != true)
            {
                writer.WritePropertyName("turn_detection"u8);
                writer.WriteObjectValue(TurnDetection, 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
                }
            }
        }

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

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

        internal static InternalRealtimeTranscriptionSessionCreateResponse DeserializeInternalRealtimeTranscriptionSessionCreateResponse(JsonElement element, ModelReaderWriterOptions options)
        {
            if (element.ValueKind == JsonValueKind.Null)
            {
                return null;
            }
            InternalRealtimeTranscriptionSessionCreateResponseClientSecret clientSecret = default;
            IList<InternalRealtimeRequestSessionModality> modalities = default;
            RealtimeAudioFormat inputAudioFormat = default;
            InputTranscriptionOptions inputAudioTranscription = default;
            TurnDetectionOptions turnDetection = default;
            IDictionary<string, BinaryData> additionalBinaryDataProperties = new ChangeTrackingDictionary<string, BinaryData>();
            foreach (var prop in element.EnumerateObject())
            {
                if (prop.NameEquals("client_secret"u8))
                {
                    clientSecret = InternalRealtimeTranscriptionSessionCreateResponseClientSecret.DeserializeInternalRealtimeTranscriptionSessionCreateResponseClientSecret(prop.Value, options);
                    continue;
                }
                if (prop.NameEquals("modalities"u8))
                {
                    if (prop.Value.ValueKind == JsonValueKind.Null)
                    {
                        continue;
                    }
                    List<InternalRealtimeRequestSessionModality> array = new List<InternalRealtimeRequestSessionModality>();
                    foreach (var item in prop.Value.EnumerateArray())
                    {
                        array.Add(new InternalRealtimeRequestSessionModality(item.GetString()));
                    }
                    modalities = array;
                    continue;
                }
                if (prop.NameEquals("input_audio_format"u8))
                {
                    DeserializeInputAudioFormatValue(prop, ref inputAudioFormat);
                    continue;
                }
                if (prop.NameEquals("input_audio_transcription"u8))
                {
                    if (prop.Value.ValueKind == JsonValueKind.Null)
                    {
                        continue;
                    }
                    inputAudioTranscription = InputTranscriptionOptions.DeserializeInputTranscriptionOptions(prop.Value, options);
                    continue;
                }
                if (prop.NameEquals("turn_detection"u8))
                {
                    if (prop.Value.ValueKind == JsonValueKind.Null)
                    {
                        continue;
                    }
                    turnDetection = TurnDetectionOptions.DeserializeTurnDetectionOptions(prop.Value, options);
                    continue;
                }
                // Plugin customization: remove options.Format != "W" check
                additionalBinaryDataProperties.Add(prop.Name, BinaryData.FromString(prop.Value.GetRawText()));
            }
            return new InternalRealtimeTranscriptionSessionCreateResponse(
                clientSecret,
                modalities ?? new ChangeTrackingList<InternalRealtimeRequestSessionModality>(),
                inputAudioFormat,
                inputAudioTranscription,
                turnDetection,
                additionalBinaryDataProperties);
        }

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

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

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

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

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