// <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
{
    public partial class TranscriptionSessionOptions : IJsonModel<TranscriptionSessionOptions>
    {
        void IJsonModel<TranscriptionSessionOptions>.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<TranscriptionSessionOptions>)this).GetFormatFromOptions(options) : options.Format;
            if (format != "J")
            {
                throw new FormatException($"The model {nameof(TranscriptionSessionOptions)} does not support writing '{format}' format.");
            }
            if (Optional.IsDefined(ClientSecret) && _additionalBinaryDataProperties?.ContainsKey("client_secret") != true)
            {
                writer.WritePropertyName("client_secret"u8);
                writer.WriteObjectValue(ClientSecret, options);
            }
            if (Optional.IsDefined(InputAudioFormat) && _additionalBinaryDataProperties?.ContainsKey("input_audio_format") != true)
            {
                writer.WritePropertyName("input_audio_format"u8);
                writer.WriteStringValue(InputAudioFormat.Value.ToString());
            }
            if (Optional.IsDefined(InputTranscriptionOptions) && _additionalBinaryDataProperties?.ContainsKey("input_audio_transcription") != true)
            {
                writer.WritePropertyName("input_audio_transcription"u8);
                writer.WriteObjectValue(InputTranscriptionOptions, options);
            }
            if (Optional.IsDefined(TurnDetectionOptions) && _additionalBinaryDataProperties?.ContainsKey("turn_detection") != true)
            {
                writer.WritePropertyName("turn_detection"u8);
                writer.WriteObjectValue(TurnDetectionOptions, options);
            }
            if (Optional.IsDefined(InputNoiseReductionOptions) && _additionalBinaryDataProperties?.ContainsKey("input_audio_noise_reduction") != true)
            {
                writer.WritePropertyName("input_audio_noise_reduction"u8);
                writer.WriteObjectValue(InputNoiseReductionOptions, options);
            }
            if (Optional.IsCollectionDefined(Include) && _additionalBinaryDataProperties?.ContainsKey("include") != true)
            {
                writer.WritePropertyName("include"u8);
                writer.WriteStartArray();
                foreach (string item in Include)
                {
                    if (item == null)
                    {
                        writer.WriteNullValue();
                        continue;
                    }
                    writer.WriteStringValue(item);
                }
                writer.WriteEndArray();
            }
            if (Optional.IsCollectionDefined(_internalModalities) && _additionalBinaryDataProperties?.ContainsKey("modalities") != true)
            {
                writer.WritePropertyName("modalities"u8);
                writer.WriteStartArray();
                foreach (InternalRealtimeRequestSessionModality item in _internalModalities)
                {
                    writer.WriteStringValue(item.ToString());
                }
                writer.WriteEndArray();
            }
            // 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
                }
            }
        }

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

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

        internal static TranscriptionSessionOptions DeserializeTranscriptionSessionOptions(JsonElement element, ModelReaderWriterOptions options)
        {
            if (element.ValueKind == JsonValueKind.Null)
            {
                return null;
            }
            InternalRealtimeTranscriptionSessionCreateRequestClientSecret clientSecret = default;
            RealtimeAudioFormat? inputAudioFormat = default;
            InputTranscriptionOptions inputTranscriptionOptions = default;
            TurnDetectionOptions turnDetectionOptions = default;
            InputNoiseReductionOptions inputNoiseReductionOptions = default;
            IList<string> include = default;
            IList<InternalRealtimeRequestSessionModality> internalModalities = default;
            IDictionary<string, BinaryData> additionalBinaryDataProperties = new ChangeTrackingDictionary<string, BinaryData>();
            foreach (var prop in element.EnumerateObject())
            {
                if (prop.NameEquals("client_secret"u8))
                {
                    if (prop.Value.ValueKind == JsonValueKind.Null)
                    {
                        continue;
                    }
                    clientSecret = InternalRealtimeTranscriptionSessionCreateRequestClientSecret.DeserializeInternalRealtimeTranscriptionSessionCreateRequestClientSecret(prop.Value, options);
                    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("input_audio_transcription"u8))
                {
                    if (prop.Value.ValueKind == JsonValueKind.Null)
                    {
                        continue;
                    }
                    inputTranscriptionOptions = InputTranscriptionOptions.DeserializeInputTranscriptionOptions(prop.Value, options);
                    continue;
                }
                if (prop.NameEquals("turn_detection"u8))
                {
                    if (prop.Value.ValueKind == JsonValueKind.Null)
                    {
                        continue;
                    }
                    turnDetectionOptions = TurnDetectionOptions.DeserializeTurnDetectionOptions(prop.Value, options);
                    continue;
                }
                if (prop.NameEquals("input_audio_noise_reduction"u8))
                {
                    if (prop.Value.ValueKind == JsonValueKind.Null)
                    {
                        inputNoiseReductionOptions = null;
                        continue;
                    }
                    inputNoiseReductionOptions = InputNoiseReductionOptions.DeserializeInputNoiseReductionOptions(prop.Value, options);
                    continue;
                }
                if (prop.NameEquals("include"u8))
                {
                    if (prop.Value.ValueKind == JsonValueKind.Null)
                    {
                        continue;
                    }
                    List<string> array = new List<string>();
                    foreach (var item in prop.Value.EnumerateArray())
                    {
                        if (item.ValueKind == JsonValueKind.Null)
                        {
                            array.Add(null);
                        }
                        else
                        {
                            array.Add(item.GetString());
                        }
                    }
                    include = array;
                    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()));
                    }
                    internalModalities = array;
                    continue;
                }
                // Plugin customization: remove options.Format != "W" check
                additionalBinaryDataProperties.Add(prop.Name, BinaryData.FromString(prop.Value.GetRawText()));
            }
            return new TranscriptionSessionOptions(
                clientSecret,
                inputAudioFormat,
                inputTranscriptionOptions,
                turnDetectionOptions,
                inputNoiseReductionOptions,
                include ?? new ChangeTrackingList<string>(),
                internalModalities,
                additionalBinaryDataProperties);
        }

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

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

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

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

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