// <auto-generated/>

#nullable disable

using System;
using System.ClientModel.Primitives;
using System.Diagnostics.CodeAnalysis;
using System.Text.Json;
using OpenAI;

namespace OpenAI.Responses
{
    [PersistableModelProxy(typeof(UnknownResponseStreamEvent))]
    public partial class StreamingResponseUpdate : IJsonModel<StreamingResponseUpdate>
    {
        internal StreamingResponseUpdate()
        {
        }

        void IJsonModel<StreamingResponseUpdate>.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<StreamingResponseUpdate>)this).GetFormatFromOptions(options) : options.Format;
            if (format != "J")
            {
                throw new FormatException($"The model {nameof(StreamingResponseUpdate)} does not support writing '{format}' format.");
            }
            if (_additionalBinaryDataProperties?.ContainsKey("type") != true)
            {
                writer.WritePropertyName("type"u8);
                writer.WriteStringValue(Kind.ToString());
            }
            if (_additionalBinaryDataProperties?.ContainsKey("sequence_number") != true)
            {
                writer.WritePropertyName("sequence_number"u8);
                writer.WriteNumberValue(SequenceNumber);
            }
            // 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
                }
            }
        }

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

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

        internal static StreamingResponseUpdate DeserializeStreamingResponseUpdate(JsonElement element, ModelReaderWriterOptions options)
        {
            if (element.ValueKind == JsonValueKind.Null)
            {
                return null;
            }
            if (element.TryGetProperty("type"u8, out JsonElement discriminator))
            {
                switch (discriminator.GetString())
                {
                    case "response.completed":
                        return StreamingResponseCompletedUpdate.DeserializeStreamingResponseCompletedUpdate(element, options);
                    case "response.content_part.added":
                        return StreamingResponseContentPartAddedUpdate.DeserializeStreamingResponseContentPartAddedUpdate(element, options);
                    case "response.content_part.done":
                        return StreamingResponseContentPartDoneUpdate.DeserializeStreamingResponseContentPartDoneUpdate(element, options);
                    case "response.created":
                        return StreamingResponseCreatedUpdate.DeserializeStreamingResponseCreatedUpdate(element, options);
                    case "error":
                        return StreamingResponseErrorUpdate.DeserializeStreamingResponseErrorUpdate(element, options);
                    case "response.file_search_call.completed":
                        return StreamingResponseFileSearchCallCompletedUpdate.DeserializeStreamingResponseFileSearchCallCompletedUpdate(element, options);
                    case "response.file_search_call.in_progress":
                        return StreamingResponseFileSearchCallInProgressUpdate.DeserializeStreamingResponseFileSearchCallInProgressUpdate(element, options);
                    case "response.file_search_call.searching":
                        return StreamingResponseFileSearchCallSearchingUpdate.DeserializeStreamingResponseFileSearchCallSearchingUpdate(element, options);
                    case "response.function_call_arguments.delta":
                        return StreamingResponseFunctionCallArgumentsDeltaUpdate.DeserializeStreamingResponseFunctionCallArgumentsDeltaUpdate(element, options);
                    case "response.function_call_arguments.done":
                        return StreamingResponseFunctionCallArgumentsDoneUpdate.DeserializeStreamingResponseFunctionCallArgumentsDoneUpdate(element, options);
                    case "response.in_progress":
                        return StreamingResponseInProgressUpdate.DeserializeStreamingResponseInProgressUpdate(element, options);
                    case "response.failed":
                        return StreamingResponseFailedUpdate.DeserializeStreamingResponseFailedUpdate(element, options);
                    case "response.incomplete":
                        return StreamingResponseIncompleteUpdate.DeserializeStreamingResponseIncompleteUpdate(element, options);
                    case "response.output_item.added":
                        return StreamingResponseOutputItemAddedUpdate.DeserializeStreamingResponseOutputItemAddedUpdate(element, options);
                    case "response.output_item.done":
                        return StreamingResponseOutputItemDoneUpdate.DeserializeStreamingResponseOutputItemDoneUpdate(element, options);
                    case "response.refusal.delta":
                        return StreamingResponseRefusalDeltaUpdate.DeserializeStreamingResponseRefusalDeltaUpdate(element, options);
                    case "response.refusal.done":
                        return StreamingResponseRefusalDoneUpdate.DeserializeStreamingResponseRefusalDoneUpdate(element, options);
                    case "response.output_text.delta":
                        return StreamingResponseOutputTextDeltaUpdate.DeserializeStreamingResponseOutputTextDeltaUpdate(element, options);
                    case "response.output_text.done":
                        return StreamingResponseOutputTextDoneUpdate.DeserializeStreamingResponseOutputTextDoneUpdate(element, options);
                    case "response.reasoning_summary_part.added":
                        return InternalResponseReasoningSummaryPartAddedEvent.DeserializeInternalResponseReasoningSummaryPartAddedEvent(element, options);
                    case "response.reasoning_summary_part.done":
                        return InternalResponseReasoningSummaryPartDoneEvent.DeserializeInternalResponseReasoningSummaryPartDoneEvent(element, options);
                    case "response.reasoning_summary_text.delta":
                        return InternalResponseReasoningSummaryTextDeltaEvent.DeserializeInternalResponseReasoningSummaryTextDeltaEvent(element, options);
                    case "response.reasoning_summary_text.done":
                        return InternalResponseReasoningSummaryTextDoneEvent.DeserializeInternalResponseReasoningSummaryTextDoneEvent(element, options);
                    case "response.web_search_call.completed":
                        return StreamingResponseWebSearchCallCompletedUpdate.DeserializeStreamingResponseWebSearchCallCompletedUpdate(element, options);
                    case "response.web_search_call.in_progress":
                        return StreamingResponseWebSearchCallInProgressUpdate.DeserializeStreamingResponseWebSearchCallInProgressUpdate(element, options);
                    case "response.web_search_call.searching":
                        return StreamingResponseWebSearchCallSearchingUpdate.DeserializeStreamingResponseWebSearchCallSearchingUpdate(element, options);
                    case "response.image_generation_call.completed":
                        return InternalResponseImageGenCallCompletedEvent.DeserializeInternalResponseImageGenCallCompletedEvent(element, options);
                    case "response.image_generation_call.generating":
                        return InternalResponseImageGenCallGeneratingEvent.DeserializeInternalResponseImageGenCallGeneratingEvent(element, options);
                    case "response.image_generation_call.in_progress":
                        return InternalResponseImageGenCallInProgressEvent.DeserializeInternalResponseImageGenCallInProgressEvent(element, options);
                    case "response.image_generation_call.partial_image":
                        return InternalResponseImageGenCallPartialImageEvent.DeserializeInternalResponseImageGenCallPartialImageEvent(element, options);
                    case "response.mcp_call.arguments_delta":
                        return InternalResponseMCPCallArgumentsDeltaEvent.DeserializeInternalResponseMCPCallArgumentsDeltaEvent(element, options);
                    case "response.mcp_call.arguments_done":
                        return InternalResponseMCPCallArgumentsDoneEvent.DeserializeInternalResponseMCPCallArgumentsDoneEvent(element, options);
                    case "response.mcp_call.completed":
                        return InternalResponseMCPCallCompletedEvent.DeserializeInternalResponseMCPCallCompletedEvent(element, options);
                    case "response.mcp_call.failed":
                        return InternalResponseMCPCallFailedEvent.DeserializeInternalResponseMCPCallFailedEvent(element, options);
                    case "response.mcp_call.in_progress":
                        return InternalResponseMCPCallInProgressEvent.DeserializeInternalResponseMCPCallInProgressEvent(element, options);
                    case "response.mcp_list_tools.completed":
                        return InternalResponseMCPListToolsCompletedEvent.DeserializeInternalResponseMCPListToolsCompletedEvent(element, options);
                    case "response.mcp_list_tools.failed":
                        return InternalResponseMCPListToolsFailedEvent.DeserializeInternalResponseMCPListToolsFailedEvent(element, options);
                    case "response.mcp_list_tools.in_progress":
                        return InternalResponseMCPListToolsInProgressEvent.DeserializeInternalResponseMCPListToolsInProgressEvent(element, options);
                    case "response.queued":
                        return StreamingResponseQueuedUpdate.DeserializeStreamingResponseQueuedUpdate(element, options);
                    case "response.reasoning.delta":
                        return InternalResponseReasoningDeltaEvent.DeserializeInternalResponseReasoningDeltaEvent(element, options);
                    case "response.reasoning.done":
                        return InternalResponseReasoningDoneEvent.DeserializeInternalResponseReasoningDoneEvent(element, options);
                    case "response.reasoning_summary.delta":
                        return InternalResponseReasoningSummaryDeltaEvent.DeserializeInternalResponseReasoningSummaryDeltaEvent(element, options);
                    case "response.reasoning_summary.done":
                        return InternalResponseReasoningSummaryDoneEvent.DeserializeInternalResponseReasoningSummaryDoneEvent(element, options);
                    case "response.code_interpreter_call_code.delta":
                        return InternalResponseCodeInterpreterCallCodeDeltaEvent.DeserializeInternalResponseCodeInterpreterCallCodeDeltaEvent(element, options);
                    case "response.code_interpreter_call_code.done":
                        return InternalResponseCodeInterpreterCallCodeDoneEvent.DeserializeInternalResponseCodeInterpreterCallCodeDoneEvent(element, options);
                    case "response.code_interpreter_call.completed":
                        return InternalResponseCodeInterpreterCallCompletedEvent.DeserializeInternalResponseCodeInterpreterCallCompletedEvent(element, options);
                    case "response.code_interpreter_call.in_progress":
                        return InternalResponseCodeInterpreterCallInProgressEvent.DeserializeInternalResponseCodeInterpreterCallInProgressEvent(element, options);
                    case "response.code_interpreter_call.interpreting":
                        return InternalResponseCodeInterpreterCallInterpretingEvent.DeserializeInternalResponseCodeInterpreterCallInterpretingEvent(element, options);
                }
            }
            return UnknownResponseStreamEvent.DeserializeUnknownResponseStreamEvent(element, options);
        }

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

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

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

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

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