// <auto-generated/>

#nullable disable

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

namespace OpenAI.Responses
{
    public partial class ReasoningResponseItem : IJsonModel<ReasoningResponseItem>
    {
        internal ReasoningResponseItem()
        {
        }

        void IJsonModel<ReasoningResponseItem>.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<ReasoningResponseItem>)this).GetFormatFromOptions(options) : options.Format;
            if (format != "J")
            {
                throw new FormatException($"The model {nameof(ReasoningResponseItem)} does not support writing '{format}' format.");
            }
            base.JsonModelWriteCore(writer, options);
            if (Optional.IsDefined(Status) && _additionalBinaryDataProperties?.ContainsKey("status") != true)
            {
                writer.WritePropertyName("status"u8);
                writer.WriteStringValue(Status.Value.ToSerialString());
            }
            if (_additionalBinaryDataProperties?.ContainsKey("summary") != true)
            {
                writer.WritePropertyName("summary"u8);
                writer.WriteStartArray();
                foreach (InternalResponsesReasoningItemSummaryElement item in Summary)
                {
                    writer.WriteObjectValue(item, options);
                }
                writer.WriteEndArray();
            }
        }

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

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

        internal static ReasoningResponseItem DeserializeReasoningResponseItem(JsonElement element, ModelReaderWriterOptions options)
        {
            if (element.ValueKind == JsonValueKind.Null)
            {
                return null;
            }
            InternalResponsesItemType @type = default;
            string id = default;
            IDictionary<string, BinaryData> additionalBinaryDataProperties = new ChangeTrackingDictionary<string, BinaryData>();
            ReasoningStatus? status = default;
            IList<InternalResponsesReasoningItemSummaryElement> summary = default;
            foreach (var prop in element.EnumerateObject())
            {
                if (prop.NameEquals("type"u8))
                {
                    @type = new InternalResponsesItemType(prop.Value.GetString());
                    continue;
                }
                if (prop.NameEquals("id"u8))
                {
                    id = prop.Value.GetString();
                    continue;
                }
                if (prop.NameEquals("status"u8))
                {
                    if (prop.Value.ValueKind == JsonValueKind.Null)
                    {
                        continue;
                    }
                    status = prop.Value.GetString().ToReasoningStatus();
                    continue;
                }
                if (prop.NameEquals("summary"u8))
                {
                    List<InternalResponsesReasoningItemSummaryElement> array = new List<InternalResponsesReasoningItemSummaryElement>();
                    foreach (var item in prop.Value.EnumerateArray())
                    {
                        array.Add(InternalResponsesReasoningItemSummaryElement.DeserializeInternalResponsesReasoningItemSummaryElement(item, options));
                    }
                    summary = array;
                    continue;
                }
                additionalBinaryDataProperties.Add(prop.Name, BinaryData.FromString(prop.Value.GetRawText()));
            }
            return new ReasoningResponseItem(@type, id, additionalBinaryDataProperties, status, summary);
        }

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

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

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

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

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

        public static implicit operator BinaryContent(ReasoningResponseItem reasoningResponseItem)
        {
            if (reasoningResponseItem == null)
            {
                return null;
            }
            return BinaryContent.Create(reasoningResponseItem, ModelSerializationExtensions.WireOptions);
        }

        public static explicit operator ReasoningResponseItem(ClientResult result)
        {
            using PipelineResponse response = result.GetRawResponse();
            using JsonDocument document = JsonDocument.Parse(response.Content);
            return DeserializeReasoningResponseItem(document.RootElement, ModelSerializationExtensions.WireOptions);
        }
    }
}
