// <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
{
    internal partial class InternalWebSearchTool : IJsonModel<InternalWebSearchTool>
    {
        void IJsonModel<InternalWebSearchTool>.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<InternalWebSearchTool>)this).GetFormatFromOptions(options) : options.Format;
            if (format != "J")
            {
                throw new FormatException($"The model {nameof(InternalWebSearchTool)} 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());
            }
        }

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

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

        internal static InternalWebSearchTool DeserializeInternalWebSearchTool(JsonElement element, ModelReaderWriterOptions options)
        {
            if (element.ValueKind == JsonValueKind.Null)
            {
                return null;
            }
            InternalToolType kind = default;
            IDictionary<string, BinaryData> additionalBinaryDataProperties = new ChangeTrackingDictionary<string, BinaryData>();
            WebSearchUserLocation userLocation = default;
            WebSearchContextSize? searchContextSize = default;
            foreach (var prop in element.EnumerateObject())
            {
                if (prop.NameEquals("type"u8))
                {
                    kind = new InternalToolType(prop.Value.GetString());
                    continue;
                }
                if (prop.NameEquals("user_location"u8))
                {
                    if (prop.Value.ValueKind == JsonValueKind.Null)
                    {
                        userLocation = null;
                        continue;
                    }
                    userLocation = WebSearchUserLocation.DeserializeWebSearchUserLocation(prop.Value, options);
                    continue;
                }
                if (prop.NameEquals("search_context_size"u8))
                {
                    if (prop.Value.ValueKind == JsonValueKind.Null)
                    {
                        continue;
                    }
                    searchContextSize = new WebSearchContextSize(prop.Value.GetString());
                    continue;
                }
                // Plugin customization: remove options.Format != "W" check
                additionalBinaryDataProperties.Add(prop.Name, BinaryData.FromString(prop.Value.GetRawText()));
            }
            return new InternalWebSearchTool(kind, additionalBinaryDataProperties, userLocation, searchContextSize);
        }

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

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

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

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

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