// <auto-generated/>

#nullable disable

using System;
using System.ClientModel.Primitives;
using System.Collections.Generic;
using System.Text.Json;
using OpenAI;

namespace OpenAI.Responses
{
    internal partial class InternalResponseReasoningSummaryPartDoneEvent : IJsonModel<InternalResponseReasoningSummaryPartDoneEvent>
    {
        internal InternalResponseReasoningSummaryPartDoneEvent() : this(InternalResponseStreamEventType.ResponseReasoningSummaryPartDone, default, null, null, default, default, null)
        {
        }

        void IJsonModel<InternalResponseReasoningSummaryPartDoneEvent>.Write(Utf8JsonWriter writer, ModelReaderWriterOptions options)
        {
            writer.WriteStartObject();
            JsonModelWriteCore(writer, options);
            writer.WriteEndObject();
        }

        protected override void JsonModelWriteCore(Utf8JsonWriter writer, ModelReaderWriterOptions options)
        {
            string format = options.Format == "W" ? ((IPersistableModel<InternalResponseReasoningSummaryPartDoneEvent>)this).GetFormatFromOptions(options) : options.Format;
            if (format != "J")
            {
                throw new FormatException($"The model {nameof(InternalResponseReasoningSummaryPartDoneEvent)} does not support writing '{format}' format.");
            }
            base.JsonModelWriteCore(writer, options);
            if (_additionalBinaryDataProperties?.ContainsKey("item_id") != true)
            {
                writer.WritePropertyName("item_id"u8);
                writer.WriteStringValue(ItemId);
            }
            if (_additionalBinaryDataProperties?.ContainsKey("output_index") != true)
            {
                writer.WritePropertyName("output_index"u8);
                writer.WriteNumberValue(OutputIndex);
            }
            if (_additionalBinaryDataProperties?.ContainsKey("summary_index") != true)
            {
                writer.WritePropertyName("summary_index"u8);
                writer.WriteNumberValue(SummaryIndex);
            }
            if (_additionalBinaryDataProperties?.ContainsKey("part") != true)
            {
                writer.WritePropertyName("part"u8);
                writer.WriteObjectValue(Part, options);
            }
        }

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

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

        internal static InternalResponseReasoningSummaryPartDoneEvent DeserializeInternalResponseReasoningSummaryPartDoneEvent(JsonElement element, ModelReaderWriterOptions options)
        {
            if (element.ValueKind == JsonValueKind.Null)
            {
                return null;
            }
            InternalResponseStreamEventType kind = default;
            int sequenceNumber = default;
            IDictionary<string, BinaryData> additionalBinaryDataProperties = new ChangeTrackingDictionary<string, BinaryData>();
            string itemId = default;
            int outputIndex = default;
            int summaryIndex = default;
            ReasoningSummaryPart part = default;
            foreach (var prop in element.EnumerateObject())
            {
                if (prop.NameEquals("type"u8))
                {
                    kind = new InternalResponseStreamEventType(prop.Value.GetString());
                    continue;
                }
                if (prop.NameEquals("sequence_number"u8))
                {
                    sequenceNumber = prop.Value.GetInt32();
                    continue;
                }
                if (prop.NameEquals("item_id"u8))
                {
                    itemId = prop.Value.GetString();
                    continue;
                }
                if (prop.NameEquals("output_index"u8))
                {
                    outputIndex = prop.Value.GetInt32();
                    continue;
                }
                if (prop.NameEquals("summary_index"u8))
                {
                    summaryIndex = prop.Value.GetInt32();
                    continue;
                }
                if (prop.NameEquals("part"u8))
                {
                    part = ReasoningSummaryPart.DeserializeReasoningSummaryPart(prop.Value, options);
                    continue;
                }
                // Plugin customization: remove options.Format != "W" check
                additionalBinaryDataProperties.Add(prop.Name, BinaryData.FromString(prop.Value.GetRawText()));
            }
            return new InternalResponseReasoningSummaryPartDoneEvent(
                kind,
                sequenceNumber,
                additionalBinaryDataProperties,
                itemId,
                outputIndex,
                summaryIndex,
                part);
        }

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

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

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

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

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