// <auto-generated/>

#nullable disable

using System;
using System.ClientModel.Primitives;
using System.Collections.Generic;
using System.Text.Json;
using OpenAI;
using OpenAI.Internal;

namespace OpenAI.Chat
{
    public partial class ChatWebSearchOptions : IJsonModel<ChatWebSearchOptions>
    {
        void IJsonModel<ChatWebSearchOptions>.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<ChatWebSearchOptions>)this).GetFormatFromOptions(options) : options.Format;
            if (format != "J")
            {
                throw new FormatException($"The model {nameof(ChatWebSearchOptions)} does not support writing '{format}' format.");
            }
            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());
            }
            // 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
                }
            }
        }

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

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

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

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

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

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

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

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