// <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.Responses
{
    public partial class ReasoningResponseItem : IJsonModel<ReasoningResponseItem>
    {
        internal ReasoningResponseItem() : this(InternalItemType.Reasoning, null, null, null, null)
        {
        }

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

        [Experimental("OPENAI001")]
        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(EncryptedContent) && _additionalBinaryDataProperties?.ContainsKey("encrypted_content") != true)
            {
                writer.WritePropertyName("encrypted_content"u8);
                writer.WriteStringValue(EncryptedContent);
            }
            if (_additionalBinaryDataProperties?.ContainsKey("summary") != true)
            {
                writer.WritePropertyName("summary"u8);
                writer.WriteStartArray();
                foreach (ReasoningSummaryPart item in SummaryParts)
                {
                    writer.WriteObjectValue(item, options);
                }
                writer.WriteEndArray();
            }
        }

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

        [Experimental("OPENAI001")]
        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;
            }
            InternalItemType kind = default;
            string id = default;
            IDictionary<string, BinaryData> additionalBinaryDataProperties = new ChangeTrackingDictionary<string, BinaryData>();
            string encryptedContent = default;
            IReadOnlyList<ReasoningSummaryPart> summaryParts = default;
            foreach (var prop in element.EnumerateObject())
            {
                if (prop.NameEquals("type"u8))
                {
                    kind = new InternalItemType(prop.Value.GetString());
                    continue;
                }
                if (prop.NameEquals("id"u8))
                {
                    id = prop.Value.GetString();
                    continue;
                }
                if (prop.NameEquals("encrypted_content"u8))
                {
                    if (prop.Value.ValueKind == JsonValueKind.Null)
                    {
                        encryptedContent = null;
                        continue;
                    }
                    encryptedContent = prop.Value.GetString();
                    continue;
                }
                if (prop.NameEquals("summary"u8))
                {
                    List<ReasoningSummaryPart> array = new List<ReasoningSummaryPart>();
                    foreach (var item in prop.Value.EnumerateArray())
                    {
                        array.Add(ReasoningSummaryPart.DeserializeReasoningSummaryPart(item, options));
                    }
                    summaryParts = array;
                    continue;
                }
                // Plugin customization: remove options.Format != "W" check
                additionalBinaryDataProperties.Add(prop.Name, BinaryData.FromString(prop.Value.GetRawText()));
            }
            return new ReasoningResponseItem(kind, id, additionalBinaryDataProperties, encryptedContent, summaryParts);
        }

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

        [Experimental("OPENAI001")]
        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, OpenAIContext.Default);
                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);

        [Experimental("OPENAI001")]
        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";
    }
}
