// <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
{
    public partial class FileSearchCallResponseItem : IJsonModel<FileSearchCallResponseItem>
    {
        internal FileSearchCallResponseItem()
        {
        }

        void IJsonModel<FileSearchCallResponseItem>.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<FileSearchCallResponseItem>)this).GetFormatFromOptions(options) : options.Format;
            if (format != "J")
            {
                throw new FormatException($"The model {nameof(FileSearchCallResponseItem)} does not support writing '{format}' format.");
            }
            base.JsonModelWriteCore(writer, options);
            if (_additionalBinaryDataProperties?.ContainsKey("status") != true)
            {
                writer.WritePropertyName("status"u8);
                writer.WriteStringValue(Status.ToSerialString());
            }
            if (_additionalBinaryDataProperties?.ContainsKey("queries") != true)
            {
                writer.WritePropertyName("queries"u8);
                writer.WriteStartArray();
                foreach (string item in Queries)
                {
                    if (item == null)
                    {
                        writer.WriteNullValue();
                        continue;
                    }
                    writer.WriteStringValue(item);
                }
                writer.WriteEndArray();
            }
            if (_additionalBinaryDataProperties?.ContainsKey("results") != true)
            {
                if (Optional.IsCollectionDefined(Results))
                {
                    writer.WritePropertyName("results"u8);
                    writer.WriteStartArray();
                    foreach (FileSearchCallResult item in Results)
                    {
                        writer.WriteObjectValue(item, options);
                    }
                    writer.WriteEndArray();
                }
                else
                {
                    writer.WriteNull("results"u8);
                }
            }
        }

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

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

        internal static FileSearchCallResponseItem DeserializeFileSearchCallResponseItem(JsonElement element, ModelReaderWriterOptions options)
        {
            if (element.ValueKind == JsonValueKind.Null)
            {
                return null;
            }
            InternalResponsesItemType @type = default;
            string id = default;
            IDictionary<string, BinaryData> additionalBinaryDataProperties = new ChangeTrackingDictionary<string, BinaryData>();
            FileSearchCallStatus status = default;
            IList<string> queries = default;
            IList<FileSearchCallResult> results = default;
            foreach (var prop in element.EnumerateObject())
            {
                if (prop.NameEquals("type"u8))
                {
                    @type = new InternalResponsesItemType(prop.Value.GetString());
                    continue;
                }
                if (prop.NameEquals("id"u8))
                {
                    id = prop.Value.GetString();
                    continue;
                }
                if (prop.NameEquals("status"u8))
                {
                    status = prop.Value.GetString().ToFileSearchCallStatus();
                    continue;
                }
                if (prop.NameEquals("queries"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());
                        }
                    }
                    queries = array;
                    continue;
                }
                if (prop.NameEquals("results"u8))
                {
                    if (prop.Value.ValueKind == JsonValueKind.Null)
                    {
                        results = new ChangeTrackingList<FileSearchCallResult>();
                        continue;
                    }
                    List<FileSearchCallResult> array = new List<FileSearchCallResult>();
                    foreach (var item in prop.Value.EnumerateArray())
                    {
                        array.Add(FileSearchCallResult.DeserializeFileSearchCallResult(item, options));
                    }
                    results = array;
                    continue;
                }
                additionalBinaryDataProperties.Add(prop.Name, BinaryData.FromString(prop.Value.GetRawText()));
            }
            return new FileSearchCallResponseItem(
                @type,
                id,
                additionalBinaryDataProperties,
                status,
                queries,
                results);
        }

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

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

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

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

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

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

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