// <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.VectorStores
{
    internal partial class InternalVectorStoreSearchRequest : IJsonModel<InternalVectorStoreSearchRequest>
    {
        internal InternalVectorStoreSearchRequest()
        {
        }

        void IJsonModel<InternalVectorStoreSearchRequest>.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<InternalVectorStoreSearchRequest>)this).GetFormatFromOptions(options) : options.Format;
            if (format != "J")
            {
                throw new FormatException($"The model {nameof(InternalVectorStoreSearchRequest)} does not support writing '{format}' format.");
            }
            if (_additionalBinaryDataProperties?.ContainsKey("query") != true)
            {
                writer.WritePropertyName("query"u8);
#if NET6_0_OR_GREATER
                writer.WriteRawValue(Query);
#else
                using (JsonDocument document = JsonDocument.Parse(Query))
                {
                    JsonSerializer.Serialize(writer, document.RootElement);
                }
#endif
            }
            if (Optional.IsDefined(RewriteQuery) && _additionalBinaryDataProperties?.ContainsKey("rewrite_query") != true)
            {
                writer.WritePropertyName("rewrite_query"u8);
                writer.WriteBooleanValue(RewriteQuery.Value);
            }
            if (Optional.IsDefined(MaxNumResults) && _additionalBinaryDataProperties?.ContainsKey("max_num_results") != true)
            {
                writer.WritePropertyName("max_num_results"u8);
                writer.WriteNumberValue(MaxNumResults.Value);
            }
            if (Optional.IsDefined(Filters) && _additionalBinaryDataProperties?.ContainsKey("filters") != true)
            {
                writer.WritePropertyName("filters"u8);
#if NET6_0_OR_GREATER
                writer.WriteRawValue(Filters);
#else
                using (JsonDocument document = JsonDocument.Parse(Filters))
                {
                    JsonSerializer.Serialize(writer, document.RootElement);
                }
#endif
            }
            if (Optional.IsDefined(RankingOptions) && _additionalBinaryDataProperties?.ContainsKey("ranking_options") != true)
            {
                writer.WritePropertyName("ranking_options"u8);
                writer.WriteObjectValue(RankingOptions, options);
            }
            // 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
                }
            }
        }

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

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

        internal static InternalVectorStoreSearchRequest DeserializeInternalVectorStoreSearchRequest(JsonElement element, ModelReaderWriterOptions options)
        {
            if (element.ValueKind == JsonValueKind.Null)
            {
                return null;
            }
            BinaryData query = default;
            bool? rewriteQuery = default;
            int? maxNumResults = default;
            BinaryData filters = default;
            InternalVectorStoreSearchRequestRankingOptions rankingOptions = default;
            IDictionary<string, BinaryData> additionalBinaryDataProperties = new ChangeTrackingDictionary<string, BinaryData>();
            foreach (var prop in element.EnumerateObject())
            {
                if (prop.NameEquals("query"u8))
                {
                    query = BinaryData.FromString(prop.Value.GetRawText());
                    continue;
                }
                if (prop.NameEquals("rewrite_query"u8))
                {
                    if (prop.Value.ValueKind == JsonValueKind.Null)
                    {
                        continue;
                    }
                    rewriteQuery = prop.Value.GetBoolean();
                    continue;
                }
                if (prop.NameEquals("max_num_results"u8))
                {
                    if (prop.Value.ValueKind == JsonValueKind.Null)
                    {
                        continue;
                    }
                    maxNumResults = prop.Value.GetInt32();
                    continue;
                }
                if (prop.NameEquals("filters"u8))
                {
                    if (prop.Value.ValueKind == JsonValueKind.Null)
                    {
                        continue;
                    }
                    filters = BinaryData.FromString(prop.Value.GetRawText());
                    continue;
                }
                if (prop.NameEquals("ranking_options"u8))
                {
                    if (prop.Value.ValueKind == JsonValueKind.Null)
                    {
                        continue;
                    }
                    rankingOptions = InternalVectorStoreSearchRequestRankingOptions.DeserializeInternalVectorStoreSearchRequestRankingOptions(prop.Value, options);
                    continue;
                }
                // Plugin customization: remove options.Format != "W" check
                additionalBinaryDataProperties.Add(prop.Name, BinaryData.FromString(prop.Value.GetRawText()));
            }
            return new InternalVectorStoreSearchRequest(
                query,
                rewriteQuery,
                maxNumResults,
                filters,
                rankingOptions,
                additionalBinaryDataProperties);
        }

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

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

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

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

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