// <auto-generated/>

#nullable disable

using System;
using System.ClientModel.Primitives;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Text.Json;
using OpenAI;
using OpenAI.Internal;

namespace OpenAI.Realtime
{
    internal partial class InternalRealtimeSessionCreateResponse : IJsonModel<InternalRealtimeSessionCreateResponse>
    {
        internal InternalRealtimeSessionCreateResponse() : this(null, null, null, default, default, default, null, default, null, null, null, null, default, null, null)
        {
        }

        void IJsonModel<InternalRealtimeSessionCreateResponse>.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<InternalRealtimeSessionCreateResponse>)this).GetFormatFromOptions(options) : options.Format;
            if (format != "J")
            {
                throw new FormatException($"The model {nameof(InternalRealtimeSessionCreateResponse)} 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 (Optional.IsDefined(Instructions) && _additionalBinaryDataProperties?.ContainsKey("instructions") != true)
            {
                writer.WritePropertyName("instructions"u8);
                writer.WriteStringValue(Instructions);
            }
            if (Optional.IsDefined(Voice) && _additionalBinaryDataProperties?.ContainsKey("voice") != true)
            {
                writer.WritePropertyName("voice"u8);
                writer.WriteStringValue(Voice.Value.ToString());
            }
            if (Optional.IsDefined(InputAudioFormat) && _additionalBinaryDataProperties?.ContainsKey("input_audio_format") != true)
            {
                writer.WritePropertyName("input_audio_format"u8);
                writer.WriteStringValue(InputAudioFormat.Value.ToString());
            }
            if (Optional.IsDefined(OutputAudioFormat) && _additionalBinaryDataProperties?.ContainsKey("output_audio_format") != true)
            {
                writer.WritePropertyName("output_audio_format"u8);
                writer.WriteStringValue(OutputAudioFormat.Value.ToString());
            }
            if (Optional.IsDefined(InputAudioTranscription) && _additionalBinaryDataProperties?.ContainsKey("input_audio_transcription") != true)
            {
                writer.WritePropertyName("input_audio_transcription"u8);
                writer.WriteObjectValue(InputAudioTranscription, options);
            }
            if (Optional.IsDefined(Speed) && _additionalBinaryDataProperties?.ContainsKey("speed") != true)
            {
                writer.WritePropertyName("speed"u8);
                writer.WriteNumberValue(Speed.Value);
            }
            if (Optional.IsDefined(Tracing) && _additionalBinaryDataProperties?.ContainsKey("tracing") != true)
            {
                writer.WritePropertyName("tracing"u8);
#if NET6_0_OR_GREATER
                writer.WriteRawValue(Tracing);
#else
                using (JsonDocument document = JsonDocument.Parse(Tracing))
                {
                    JsonSerializer.Serialize(writer, document.RootElement);
                }
#endif
            }
            if (Optional.IsDefined(TurnDetection) && _additionalBinaryDataProperties?.ContainsKey("turn_detection") != true)
            {
                writer.WritePropertyName("turn_detection"u8);
                writer.WriteObjectValue(TurnDetection, options);
            }
            if (Optional.IsCollectionDefined(Tools) && _additionalBinaryDataProperties?.ContainsKey("tools") != true)
            {
                writer.WritePropertyName("tools"u8);
                writer.WriteStartArray();
                foreach (ConversationTool item in Tools)
                {
                    writer.WriteObjectValue(item, options);
                }
                writer.WriteEndArray();
            }
            if (Optional.IsDefined(ToolChoice) && _additionalBinaryDataProperties?.ContainsKey("tool_choice") != true)
            {
                writer.WritePropertyName("tool_choice"u8);
                writer.WriteStringValue(ToolChoice);
            }
            if (Optional.IsDefined(Temperature) && _additionalBinaryDataProperties?.ContainsKey("temperature") != true)
            {
                writer.WritePropertyName("temperature"u8);
                writer.WriteNumberValue(Temperature.Value);
            }
            if (Optional.IsDefined(MaxResponseOutputTokens) && _additionalBinaryDataProperties?.ContainsKey("max_response_output_tokens") != true)
            {
                writer.WritePropertyName("max_response_output_tokens"u8);
#if NET6_0_OR_GREATER
                writer.WriteRawValue(MaxResponseOutputTokens);
#else
                using (JsonDocument document = JsonDocument.Parse(MaxResponseOutputTokens))
                {
                    JsonSerializer.Serialize(writer, document.RootElement);
                }
#endif
            }
            // 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
                }
            }
        }

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

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

        internal static InternalRealtimeSessionCreateResponse DeserializeInternalRealtimeSessionCreateResponse(JsonElement element, ModelReaderWriterOptions options)
        {
            if (element.ValueKind == JsonValueKind.Null)
            {
                return null;
            }
            InternalRealtimeSessionCreateResponseClientSecret clientSecret = default;
            IList<InternalRealtimeRequestSessionModality> modalities = default;
            string instructions = default;
            InternalVoiceIdsShared? voice = default;
            RealtimeAudioFormat? inputAudioFormat = default;
            RealtimeAudioFormat? outputAudioFormat = default;
            InternalRealtimeSessionCreateResponseInputAudioTranscription inputAudioTranscription = default;
            float? speed = default;
            BinaryData tracing = default;
            InternalRealtimeSessionCreateResponseTurnDetection turnDetection = default;
            IList<ConversationTool> tools = default;
            string toolChoice = default;
            float? temperature = default;
            BinaryData maxResponseOutputTokens = default;
            IDictionary<string, BinaryData> additionalBinaryDataProperties = new ChangeTrackingDictionary<string, BinaryData>();
            foreach (var prop in element.EnumerateObject())
            {
                if (prop.NameEquals("client_secret"u8))
                {
                    clientSecret = InternalRealtimeSessionCreateResponseClientSecret.DeserializeInternalRealtimeSessionCreateResponseClientSecret(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("instructions"u8))
                {
                    instructions = prop.Value.GetString();
                    continue;
                }
                if (prop.NameEquals("voice"u8))
                {
                    if (prop.Value.ValueKind == JsonValueKind.Null)
                    {
                        continue;
                    }
                    voice = new InternalVoiceIdsShared(prop.Value.GetString());
                    continue;
                }
                if (prop.NameEquals("input_audio_format"u8))
                {
                    if (prop.Value.ValueKind == JsonValueKind.Null)
                    {
                        continue;
                    }
                    inputAudioFormat = new RealtimeAudioFormat(prop.Value.GetString());
                    continue;
                }
                if (prop.NameEquals("output_audio_format"u8))
                {
                    if (prop.Value.ValueKind == JsonValueKind.Null)
                    {
                        continue;
                    }
                    outputAudioFormat = new RealtimeAudioFormat(prop.Value.GetString());
                    continue;
                }
                if (prop.NameEquals("input_audio_transcription"u8))
                {
                    if (prop.Value.ValueKind == JsonValueKind.Null)
                    {
                        continue;
                    }
                    inputAudioTranscription = InternalRealtimeSessionCreateResponseInputAudioTranscription.DeserializeInternalRealtimeSessionCreateResponseInputAudioTranscription(prop.Value, options);
                    continue;
                }
                if (prop.NameEquals("speed"u8))
                {
                    if (prop.Value.ValueKind == JsonValueKind.Null)
                    {
                        continue;
                    }
                    speed = prop.Value.GetSingle();
                    continue;
                }
                if (prop.NameEquals("tracing"u8))
                {
                    if (prop.Value.ValueKind == JsonValueKind.Null)
                    {
                        continue;
                    }
                    tracing = BinaryData.FromString(prop.Value.GetRawText());
                    continue;
                }
                if (prop.NameEquals("turn_detection"u8))
                {
                    if (prop.Value.ValueKind == JsonValueKind.Null)
                    {
                        continue;
                    }
                    turnDetection = InternalRealtimeSessionCreateResponseTurnDetection.DeserializeInternalRealtimeSessionCreateResponseTurnDetection(prop.Value, options);
                    continue;
                }
                if (prop.NameEquals("tools"u8))
                {
                    if (prop.Value.ValueKind == JsonValueKind.Null)
                    {
                        continue;
                    }
                    List<ConversationTool> array = new List<ConversationTool>();
                    foreach (var item in prop.Value.EnumerateArray())
                    {
                        array.Add(ConversationTool.DeserializeConversationTool(item, options));
                    }
                    tools = array;
                    continue;
                }
                if (prop.NameEquals("tool_choice"u8))
                {
                    toolChoice = prop.Value.GetString();
                    continue;
                }
                if (prop.NameEquals("temperature"u8))
                {
                    if (prop.Value.ValueKind == JsonValueKind.Null)
                    {
                        continue;
                    }
                    temperature = prop.Value.GetSingle();
                    continue;
                }
                if (prop.NameEquals("max_response_output_tokens"u8))
                {
                    if (prop.Value.ValueKind == JsonValueKind.Null)
                    {
                        continue;
                    }
                    maxResponseOutputTokens = BinaryData.FromString(prop.Value.GetRawText());
                    continue;
                }
                // Plugin customization: remove options.Format != "W" check
                additionalBinaryDataProperties.Add(prop.Name, BinaryData.FromString(prop.Value.GetRawText()));
            }
            return new InternalRealtimeSessionCreateResponse(
                clientSecret,
                modalities ?? new ChangeTrackingList<InternalRealtimeRequestSessionModality>(),
                instructions,
                voice,
                inputAudioFormat,
                outputAudioFormat,
                inputAudioTranscription,
                speed,
                tracing,
                turnDetection,
                tools ?? new ChangeTrackingList<ConversationTool>(),
                toolChoice,
                temperature,
                maxResponseOutputTokens,
                additionalBinaryDataProperties);
        }

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

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

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

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

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