// <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.Responses
{
    internal partial class InternalCodeInterpreterToolCallItemParam : IJsonModel<InternalCodeInterpreterToolCallItemParam>
    {
        internal InternalCodeInterpreterToolCallItemParam() : this(InternalItemType.CodeInterpreterCall, null, null, null, null)
        {
        }

        void IJsonModel<InternalCodeInterpreterToolCallItemParam>.Write(Utf8JsonWriter writer, ModelReaderWriterOptions options)
        {
            writer.WriteStartObject();
            JsonModelWriteCore(writer, options);
            writer.WriteEndObject();
        }

        [Experimental("OPENAI001")]
        protected override void JsonModelWriteCore(Utf8JsonWriter writer, ModelReaderWriterOptions options)
        {
            string format = options.Format == "W" ? ((IPersistableModel<InternalCodeInterpreterToolCallItemParam>)this).GetFormatFromOptions(options) : options.Format;
            if (format != "J")
            {
                throw new FormatException($"The model {nameof(InternalCodeInterpreterToolCallItemParam)} does not support writing '{format}' format.");
            }
            base.JsonModelWriteCore(writer, options);
            if (Optional.IsDefined(ContainerId) && _additionalBinaryDataProperties?.ContainsKey("container_id") != true)
            {
                writer.WritePropertyName("container_id"u8);
                writer.WriteStringValue(ContainerId);
            }
            if (_additionalBinaryDataProperties?.ContainsKey("code") != true)
            {
                writer.WritePropertyName("code"u8);
                writer.WriteStringValue(Code);
            }
            if (_additionalBinaryDataProperties?.ContainsKey("results") != true)
            {
                writer.WritePropertyName("results"u8);
                writer.WriteStartArray();
                foreach (BinaryData item in Results)
                {
                    if (item == null)
                    {
                        writer.WriteNullValue();
                        continue;
                    }
#if NET6_0_OR_GREATER
                    writer.WriteRawValue(item);
#else
                    using (JsonDocument document = JsonDocument.Parse(item))
                    {
                        JsonSerializer.Serialize(writer, document.RootElement);
                    }
#endif
                }
                writer.WriteEndArray();
            }
        }

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

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

        internal static InternalCodeInterpreterToolCallItemParam DeserializeInternalCodeInterpreterToolCallItemParam(JsonElement element, ModelReaderWriterOptions options)
        {
            if (element.ValueKind == JsonValueKind.Null)
            {
                return null;
            }
            InternalItemType kind = default;
            IDictionary<string, BinaryData> additionalBinaryDataProperties = new ChangeTrackingDictionary<string, BinaryData>();
            string containerId = default;
            string code = default;
            IList<BinaryData> results = default;
            foreach (var prop in element.EnumerateObject())
            {
                if (prop.NameEquals("type"u8))
                {
                    kind = new InternalItemType(prop.Value.GetString());
                    continue;
                }
                if (prop.NameEquals("container_id"u8))
                {
                    containerId = prop.Value.GetString();
                    continue;
                }
                if (prop.NameEquals("code"u8))
                {
                    code = prop.Value.GetString();
                    continue;
                }
                if (prop.NameEquals("results"u8))
                {
                    List<BinaryData> array = new List<BinaryData>();
                    foreach (var item in prop.Value.EnumerateArray())
                    {
                        if (item.ValueKind == JsonValueKind.Null)
                        {
                            array.Add(null);
                        }
                        else
                        {
                            array.Add(BinaryData.FromString(item.GetRawText()));
                        }
                    }
                    results = array;
                    continue;
                }
                // Plugin customization: remove options.Format != "W" check
                additionalBinaryDataProperties.Add(prop.Name, BinaryData.FromString(prop.Value.GetRawText()));
            }
            return new InternalCodeInterpreterToolCallItemParam(kind, additionalBinaryDataProperties, containerId, code, results);
        }

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

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

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

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

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