// <auto-generated/>

#nullable disable

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

namespace OpenAI.Responses
{
    internal partial class InternalFileSearchTool : IJsonModel<InternalFileSearchTool>
    {
        internal InternalFileSearchTool() : this(InternalToolType.FileSearch, null, null, default, null, null)
        {
        }

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

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

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

        internal static InternalFileSearchTool DeserializeInternalFileSearchTool(JsonElement element, ModelReaderWriterOptions options)
        {
            if (element.ValueKind == JsonValueKind.Null)
            {
                return null;
            }
            InternalToolType kind = 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))
                {
                    kind = new InternalToolType(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)
                    {
                        filters = null;
                        continue;
                    }
                    filters = BinaryData.FromString(prop.Value.GetRawText());
                    continue;
                }
                // Plugin customization: remove options.Format != "W" check
                additionalBinaryDataProperties.Add(prop.Name, BinaryData.FromString(prop.Value.GetRawText()));
            }
            return new InternalFileSearchTool(
                kind,
                additionalBinaryDataProperties,
                vectorStoreIds,
                maxNumResults,
                rankingOptions,
                filters);
        }

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

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

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

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

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