// <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 InternalFunctionTool : IJsonModel<InternalFunctionTool>
    {
        internal InternalFunctionTool() : this(InternalToolType.Function, null, null, null, default, null)
        {
        }

        void IJsonModel<InternalFunctionTool>.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<InternalFunctionTool>)this).GetFormatFromOptions(options) : options.Format;
            if (format != "J")
            {
                throw new FormatException($"The model {nameof(InternalFunctionTool)} does not support writing '{format}' format.");
            }
            base.JsonModelWriteCore(writer, options);
            if (_additionalBinaryDataProperties?.ContainsKey("name") != true)
            {
                writer.WritePropertyName("name"u8);
                writer.WriteStringValue(Name);
            }
            if (Optional.IsDefined(Description) && _additionalBinaryDataProperties?.ContainsKey("description") != true)
            {
                writer.WritePropertyName("description"u8);
                writer.WriteStringValue(Description);
            }
            if (_additionalBinaryDataProperties?.ContainsKey("strict") != true)
            {
                if (Optional.IsDefined(Strict))
                {
                    writer.WritePropertyName("strict"u8);
                    writer.WriteBooleanValue(Strict.Value);
                }
                else
                {
                    writer.WriteNull("strict"u8);
                }
            }
            if (_additionalBinaryDataProperties?.ContainsKey("parameters") != true)
            {
                if (Optional.IsDefined(Parameters))
                {
                    writer.WritePropertyName("parameters"u8);
#if NET6_0_OR_GREATER
                    writer.WriteRawValue(Parameters);
#else
                    using (JsonDocument document = JsonDocument.Parse(Parameters))
                    {
                        JsonSerializer.Serialize(writer, document.RootElement);
                    }
#endif
                }
                else
                {
                    writer.WriteNull("parameters"u8);
                }
            }
        }

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

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

        internal static InternalFunctionTool DeserializeInternalFunctionTool(JsonElement element, ModelReaderWriterOptions options)
        {
            if (element.ValueKind == JsonValueKind.Null)
            {
                return null;
            }
            InternalToolType kind = default;
            IDictionary<string, BinaryData> additionalBinaryDataProperties = new ChangeTrackingDictionary<string, BinaryData>();
            string name = default;
            string description = default;
            bool? strict = default;
            BinaryData parameters = default;
            foreach (var prop in element.EnumerateObject())
            {
                if (prop.NameEquals("type"u8))
                {
                    kind = new InternalToolType(prop.Value.GetString());
                    continue;
                }
                if (prop.NameEquals("name"u8))
                {
                    name = prop.Value.GetString();
                    continue;
                }
                if (prop.NameEquals("description"u8))
                {
                    if (prop.Value.ValueKind == JsonValueKind.Null)
                    {
                        description = null;
                        continue;
                    }
                    description = prop.Value.GetString();
                    continue;
                }
                if (prop.NameEquals("strict"u8))
                {
                    if (prop.Value.ValueKind == JsonValueKind.Null)
                    {
                        strict = null;
                        continue;
                    }
                    strict = prop.Value.GetBoolean();
                    continue;
                }
                if (prop.NameEquals("parameters"u8))
                {
                    if (prop.Value.ValueKind == JsonValueKind.Null)
                    {
                        parameters = null;
                        continue;
                    }
                    parameters = BinaryData.FromString(prop.Value.GetRawText());
                    continue;
                }
                // Plugin customization: remove options.Format != "W" check
                additionalBinaryDataProperties.Add(prop.Name, BinaryData.FromString(prop.Value.GetRawText()));
            }
            return new InternalFunctionTool(
                kind,
                additionalBinaryDataProperties,
                name,
                description,
                strict,
                parameters);
        }

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

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

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

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

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