// <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(InternalUnknownItemResource))]
    public partial class ResponseItem : IJsonModel<ResponseItem>
    {
        internal ResponseItem()
        {
        }

        void IJsonModel<ResponseItem>.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<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(Kind.ToString());
            }
            // Plugin customization: apply Optional.Is*Defined() check based on type name dictionary lookup
            if (Optional.IsDefined(Id) && _additionalBinaryDataProperties?.ContainsKey("id") != true)
            {
                writer.WritePropertyName("id"u8);
                writer.WriteStringValue(Id);
            }
            // 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
                }
            }
        }

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

        [Experimental("OPENAI001")]
        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 "item_reference":
                        return ReferenceResponseItem.DeserializeReferenceResponseItem(element, options);
                    case "file_search_call":
                        return FileSearchCallResponseItem.DeserializeFileSearchCallResponseItem(element, options);
                    case "computer_call":
                        return ComputerCallResponseItem.DeserializeComputerCallResponseItem(element, options);
                    case "computer_call_output":
                        return ComputerCallOutputResponseItem.DeserializeComputerCallOutputResponseItem(element, options);
                    case "web_search_call":
                        return WebSearchCallResponseItem.DeserializeWebSearchCallResponseItem(element, options);
                    case "function_call":
                        return FunctionCallResponseItem.DeserializeFunctionCallResponseItem(element, options);
                    case "function_call_output":
                        return FunctionCallOutputResponseItem.DeserializeFunctionCallOutputResponseItem(element, options);
                    case "reasoning":
                        return ReasoningResponseItem.DeserializeReasoningResponseItem(element, options);
                    case "image_generation_call":
                        return InternalImageGenToolCallItemResource.DeserializeInternalImageGenToolCallItemResource(element, options);
                    case "code_interpreter_call":
                        return InternalCodeInterpreterToolCallItemResource.DeserializeInternalCodeInterpreterToolCallItemResource(element, options);
                    case "local_shell_call":
                        return InternalLocalShellToolCallItemResource.DeserializeInternalLocalShellToolCallItemResource(element, options);
                    case "local_shell_call_output":
                        return InternalLocalShellToolCallOutputItemResource.DeserializeInternalLocalShellToolCallOutputItemResource(element, options);
                    case "mcp_list_tools":
                        return InternalMCPListToolsItemResource.DeserializeInternalMCPListToolsItemResource(element, options);
                    case "mcp_approval_request":
                        return InternalMCPApprovalRequestItemResource.DeserializeInternalMCPApprovalRequestItemResource(element, options);
                    case "mcp_approval_response":
                        return InternalMCPApprovalResponseItemResource.DeserializeInternalMCPApprovalResponseItemResource(element, options);
                    case "mcp_call":
                        return InternalMCPCallItemResource.DeserializeInternalMCPCallItemResource(element, options);
                    case "message":
                        return MessageResponseItem.DeserializeMessageResponseItem(element, options);
                }
            }
            return InternalUnknownItemResource.DeserializeInternalUnknownItemResource(element, options);
        }

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

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

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