// <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
{
    internal partial class InternalResponsesWebSearchTool : IJsonModel<InternalResponsesWebSearchTool>
    {
        void IJsonModel<InternalResponsesWebSearchTool>.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<InternalResponsesWebSearchTool>)this).GetFormatFromOptions(options) : options.Format;
            if (format != "J")
            {
                throw new FormatException($"The model {nameof(InternalResponsesWebSearchTool)} does not support writing '{format}' format.");
            }
            base.JsonModelWriteCore(writer, options);
            if (Optional.IsDefined(UserLocation) && _additionalBinaryDataProperties?.ContainsKey("user_location") != true)
            {
                writer.WritePropertyName("user_location"u8);
                writer.WriteObjectValue(UserLocation, options);
            }
            if (Optional.IsDefined(SearchContextSize) && _additionalBinaryDataProperties?.ContainsKey("search_context_size") != true)
            {
                writer.WritePropertyName("search_context_size"u8);
                writer.WriteStringValue(SearchContextSize.Value.ToString());
            }
        }

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

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

        internal static InternalResponsesWebSearchTool DeserializeInternalResponsesWebSearchTool(JsonElement element, ModelReaderWriterOptions options)
        {
            if (element.ValueKind == JsonValueKind.Null)
            {
                return null;
            }
            InternalResponsesToolType @type = default;
            IDictionary<string, BinaryData> additionalBinaryDataProperties = new ChangeTrackingDictionary<string, BinaryData>();
            WebSearchToolLocation userLocation = default;
            WebSearchToolContextSize? searchContextSize = default;
            foreach (var prop in element.EnumerateObject())
            {
                if (prop.NameEquals("type"u8))
                {
                    @type = new InternalResponsesToolType(prop.Value.GetString());
                    continue;
                }
                if (prop.NameEquals("user_location"u8))
                {
                    if (prop.Value.ValueKind == JsonValueKind.Null)
                    {
                        continue;
                    }
                    userLocation = WebSearchToolLocation.DeserializeWebSearchToolLocation(prop.Value, options);
                    continue;
                }
                if (prop.NameEquals("search_context_size"u8))
                {
                    if (prop.Value.ValueKind == JsonValueKind.Null)
                    {
                        continue;
                    }
                    searchContextSize = new WebSearchToolContextSize(prop.Value.GetString());
                    continue;
                }
                additionalBinaryDataProperties.Add(prop.Name, BinaryData.FromString(prop.Value.GetRawText()));
            }
            return new InternalResponsesWebSearchTool(@type, additionalBinaryDataProperties, userLocation, searchContextSize);
        }

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

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

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

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

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

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

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