// <auto-generated/>

#nullable disable

using System;
using System.ClientModel;
using System.ClientModel.Primitives;
using System.Text.Json;

namespace OpenAI.RealtimeConversation
{
    [PersistableModelProxy(typeof(UnknownRealtimeRequestCommand))]
    internal partial class InternalRealtimeRequestCommand : IJsonModel<InternalRealtimeRequestCommand>
    {
        void IJsonModel<InternalRealtimeRequestCommand>.Write(Utf8JsonWriter writer, ModelReaderWriterOptions options)
        {
            var format = options.Format == "W" ? ((IPersistableModel<InternalRealtimeRequestCommand>)this).GetFormatFromOptions(options) : options.Format;
            if (format != "J")
            {
                throw new FormatException($"The model {nameof(InternalRealtimeRequestCommand)} does not support writing '{format}' format.");
            }

            writer.WriteStartObject();
            if (SerializedAdditionalRawData?.ContainsKey("type") != true)
            {
                writer.WritePropertyName("type"u8);
                writer.WriteStringValue(Kind.ToString());
            }
            if (SerializedAdditionalRawData?.ContainsKey("event_id") != true && Optional.IsDefined(EventId))
            {
                writer.WritePropertyName("event_id"u8);
                writer.WriteStringValue(EventId);
            }
            if (SerializedAdditionalRawData != null)
            {
                foreach (var item in SerializedAdditionalRawData)
                {
                    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
                }
            }
            writer.WriteEndObject();
        }

        InternalRealtimeRequestCommand IJsonModel<InternalRealtimeRequestCommand>.Create(ref Utf8JsonReader reader, ModelReaderWriterOptions options)
        {
            var format = options.Format == "W" ? ((IPersistableModel<InternalRealtimeRequestCommand>)this).GetFormatFromOptions(options) : options.Format;
            if (format != "J")
            {
                throw new FormatException($"The model {nameof(InternalRealtimeRequestCommand)} does not support reading '{format}' format.");
            }

            using JsonDocument document = JsonDocument.ParseValue(ref reader);
            return DeserializeInternalRealtimeRequestCommand(document.RootElement, options);
        }

        internal static InternalRealtimeRequestCommand DeserializeInternalRealtimeRequestCommand(JsonElement element, ModelReaderWriterOptions options = null)
        {
            options ??= ModelSerializationExtensions.WireOptions;

            if (element.ValueKind == JsonValueKind.Null)
            {
                return null;
            }
            if (element.TryGetProperty("type", out JsonElement discriminator))
            {
                switch (discriminator.GetString())
                {
                    case "conversation.item.create": return InternalRealtimeRequestItemCreateCommand.DeserializeInternalRealtimeRequestItemCreateCommand(element, options);
                    case "conversation.item.delete": return InternalRealtimeRequestItemDeleteCommand.DeserializeInternalRealtimeRequestItemDeleteCommand(element, options);
                    case "conversation.item.truncate": return InternalRealtimeRequestItemTruncateCommand.DeserializeInternalRealtimeRequestItemTruncateCommand(element, options);
                    case "input_audio_buffer.append": return InternalRealtimeRequestInputAudioBufferAppendCommand.DeserializeInternalRealtimeRequestInputAudioBufferAppendCommand(element, options);
                    case "input_audio_buffer.clear": return InternalRealtimeRequestInputAudioBufferClearCommand.DeserializeInternalRealtimeRequestInputAudioBufferClearCommand(element, options);
                    case "input_audio_buffer.commit": return InternalRealtimeRequestInputAudioBufferCommitCommand.DeserializeInternalRealtimeRequestInputAudioBufferCommitCommand(element, options);
                    case "response.cancel": return InternalRealtimeRequestResponseCancelCommand.DeserializeInternalRealtimeRequestResponseCancelCommand(element, options);
                    case "response.create": return InternalRealtimeRequestResponseCreateCommand.DeserializeInternalRealtimeRequestResponseCreateCommand(element, options);
                    case "session.update": return InternalRealtimeRequestSessionUpdateCommand.DeserializeInternalRealtimeRequestSessionUpdateCommand(element, options);
                }
            }
            return UnknownRealtimeRequestCommand.DeserializeUnknownRealtimeRequestCommand(element, options);
        }

        BinaryData IPersistableModel<InternalRealtimeRequestCommand>.Write(ModelReaderWriterOptions options)
        {
            var format = options.Format == "W" ? ((IPersistableModel<InternalRealtimeRequestCommand>)this).GetFormatFromOptions(options) : options.Format;

            switch (format)
            {
                case "J":
                    return ModelReaderWriter.Write(this, options);
                default:
                    throw new FormatException($"The model {nameof(InternalRealtimeRequestCommand)} does not support writing '{options.Format}' format.");
            }
        }

        InternalRealtimeRequestCommand IPersistableModel<InternalRealtimeRequestCommand>.Create(BinaryData data, ModelReaderWriterOptions options)
        {
            var format = options.Format == "W" ? ((IPersistableModel<InternalRealtimeRequestCommand>)this).GetFormatFromOptions(options) : options.Format;

            switch (format)
            {
                case "J":
                    {
                        using JsonDocument document = JsonDocument.Parse(data);
                        return DeserializeInternalRealtimeRequestCommand(document.RootElement, options);
                    }
                default:
                    throw new FormatException($"The model {nameof(InternalRealtimeRequestCommand)} does not support reading '{options.Format}' format.");
            }
        }

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

        internal static InternalRealtimeRequestCommand FromResponse(PipelineResponse response)
        {
            using var document = JsonDocument.Parse(response.Content);
            return DeserializeInternalRealtimeRequestCommand(document.RootElement);
        }

        internal virtual BinaryContent ToBinaryContent()
        {
            return BinaryContent.Create(this, ModelSerializationExtensions.WireOptions);
        }
    }
}
