// <auto-generated/>

#nullable disable

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

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

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

        protected virtual void JsonModelWriteCore(Utf8JsonWriter writer, ModelReaderWriterOptions options)
        {
            string format = options.Format == "W" ? ((IPersistableModel<ResponseItem>)this).GetFormatFromOptions(options) : options.Format;
            if (format != "J")
            {
                throw new FormatException($"The model {nameof(ResponseItem)} does not support writing '{format}' format.");
            }
            if (_additionalBinaryDataProperties?.ContainsKey("type") != true)
            {
                writer.WritePropertyName("type"u8);
                writer.WriteStringValue(Type.ToString());
            }
            if (Optional.IsDefined(Id) && _additionalBinaryDataProperties?.ContainsKey("id") != true)
            {
                writer.WritePropertyName("id"u8);
                writer.WriteStringValue(Id);
            }
            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
                }
            }
        }

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

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

        internal static ResponseItem DeserializeResponseItem(JsonElement element, ModelReaderWriterOptions options)
        {
            if (element.ValueKind == JsonValueKind.Null)
            {
                return null;
            }
            if (element.TryGetProperty("type"u8, out JsonElement discriminator))
            {
                switch (discriminator.GetString())
                {
                    case "message":
                        return MessageResponseItem.DeserializeMessageResponseItem(element, options);
                    case "function_call":
                        return FunctionCallResponseItem.DeserializeFunctionCallResponseItem(element, options);
                    case "function_call_output":
                        return FunctionCallOutputResponseItem.DeserializeFunctionCallOutputResponseItem(element, options);
                    case "computer_call":
                        return ComputerCallResponseItem.DeserializeComputerCallResponseItem(element, options);
                    case "computer_call_output":
                        return ComputerCallOutputResponseItem.DeserializeComputerCallOutputResponseItem(element, options);
                    case "file_search_call":
                        return FileSearchCallResponseItem.DeserializeFileSearchCallResponseItem(element, options);
                    case "item_reference":
                        return ReferenceResponseItem.DeserializeReferenceResponseItem(element, options);
                    case "web_search_call":
                        return WebSearchCallResponseItem.DeserializeWebSearchCallResponseItem(element, options);
                    case "reasoning":
                        return ReasoningResponseItem.DeserializeReasoningResponseItem(element, options);
                }
            }
            return InternalUnknownResponsesItem.DeserializeInternalUnknownResponsesItem(element, options);
        }

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

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

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

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

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

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

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