// <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 InternalResponsesFileSearchTool : IJsonModel<InternalResponsesFileSearchTool>
    {
        internal InternalResponsesFileSearchTool()
        {
        }

        void IJsonModel<InternalResponsesFileSearchTool>.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<InternalResponsesFileSearchTool>)this).GetFormatFromOptions(options) : options.Format;
            if (format != "J")
            {
                throw new FormatException($"The model {nameof(InternalResponsesFileSearchTool)} does not support writing '{format}' format.");
            }
            base.JsonModelWriteCore(writer, options);
            if (_additionalBinaryDataProperties?.ContainsKey("vector_store_ids") != true)
            {
                writer.WritePropertyName("vector_store_ids"u8);
                writer.WriteStartArray();
                foreach (string item in VectorStoreIds)
                {
                    if (item == null)
                    {
                        writer.WriteNullValue();
                        continue;
                    }
                    writer.WriteStringValue(item);
                }
                writer.WriteEndArray();
            }
            if (Optional.IsDefined(MaxNumResults) && _additionalBinaryDataProperties?.ContainsKey("max_num_results") != true)
            {
                writer.WritePropertyName("max_num_results"u8);
                writer.WriteNumberValue(MaxNumResults.Value);
            }
            if (Optional.IsDefined(RankingOptions) && _additionalBinaryDataProperties?.ContainsKey("ranking_options") != true)
            {
                writer.WritePropertyName("ranking_options"u8);
                writer.WriteObjectValue(RankingOptions, options);
            }
            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
            }
        }

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

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

        internal static InternalResponsesFileSearchTool DeserializeInternalResponsesFileSearchTool(JsonElement element, ModelReaderWriterOptions options)
        {
            if (element.ValueKind == JsonValueKind.Null)
            {
                return null;
            }
            InternalResponsesToolType @type = default;
            IDictionary<string, BinaryData> additionalBinaryDataProperties = new ChangeTrackingDictionary<string, BinaryData>();
            IList<string> vectorStoreIds = default;
            int? maxNumResults = default;
            FileSearchToolRankingOptions rankingOptions = default;
            BinaryData filters = default;
            foreach (var prop in element.EnumerateObject())
            {
                if (prop.NameEquals("type"u8))
                {
                    @type = new InternalResponsesToolType(prop.Value.GetString());
                    continue;
                }
                if (prop.NameEquals("vector_store_ids"u8))
                {
                    List<string> array = new List<string>();
                    foreach (var item in prop.Value.EnumerateArray())
                    {
                        if (item.ValueKind == JsonValueKind.Null)
                        {
                            array.Add(null);
                        }
                        else
                        {
                            array.Add(item.GetString());
                        }
                    }
                    vectorStoreIds = array;
                    continue;
                }
                if (prop.NameEquals("max_num_results"u8))
                {
                    if (prop.Value.ValueKind == JsonValueKind.Null)
                    {
                        continue;
                    }
                    maxNumResults = prop.Value.GetInt32();
                    continue;
                }
                if (prop.NameEquals("ranking_options"u8))
                {
                    if (prop.Value.ValueKind == JsonValueKind.Null)
                    {
                        continue;
                    }
                    rankingOptions = FileSearchToolRankingOptions.DeserializeFileSearchToolRankingOptions(prop.Value, options);
                    continue;
                }
                if (prop.NameEquals("filters"u8))
                {
                    if (prop.Value.ValueKind == JsonValueKind.Null)
                    {
                        continue;
                    }
                    filters = BinaryData.FromString(prop.Value.GetRawText());
                    continue;
                }
                additionalBinaryDataProperties.Add(prop.Name, BinaryData.FromString(prop.Value.GetRawText()));
            }
            return new InternalResponsesFileSearchTool(
                @type,
                additionalBinaryDataProperties,
                vectorStoreIds,
                maxNumResults,
                rankingOptions,
                filters);
        }

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

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

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

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

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

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

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