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

        void IJsonModel<InternalMCPTool>.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<InternalMCPTool>)this).GetFormatFromOptions(options) : options.Format;
            if (format != "J")
            {
                throw new FormatException($"The model {nameof(InternalMCPTool)} does not support writing '{format}' format.");
            }
            base.JsonModelWriteCore(writer, options);
            if (_additionalBinaryDataProperties?.ContainsKey("server_label") != true)
            {
                writer.WritePropertyName("server_label"u8);
                writer.WriteStringValue(ServerLabel);
            }
            if (_additionalBinaryDataProperties?.ContainsKey("server_url") != true)
            {
                writer.WritePropertyName("server_url"u8);
                writer.WriteStringValue(ServerUrl);
            }
            if (Optional.IsCollectionDefined(Headers) && _additionalBinaryDataProperties?.ContainsKey("headers") != true)
            {
                writer.WritePropertyName("headers"u8);
                writer.WriteStartObject();
                foreach (var item in Headers)
                {
                    writer.WritePropertyName(item.Key);
                    if (item.Value == null)
                    {
                        writer.WriteNullValue();
                        continue;
                    }
                    writer.WriteStringValue(item.Value);
                }
                writer.WriteEndObject();
            }
            if (Optional.IsDefined(AllowedTools) && _additionalBinaryDataProperties?.ContainsKey("allowed_tools") != true)
            {
                writer.WritePropertyName("allowed_tools"u8);
#if NET6_0_OR_GREATER
                writer.WriteRawValue(AllowedTools);
#else
                using (JsonDocument document = JsonDocument.Parse(AllowedTools))
                {
                    JsonSerializer.Serialize(writer, document.RootElement);
                }
#endif
            }
            if (Optional.IsDefined(RequireApproval) && _additionalBinaryDataProperties?.ContainsKey("require_approval") != true)
            {
                writer.WritePropertyName("require_approval"u8);
#if NET6_0_OR_GREATER
                writer.WriteRawValue(RequireApproval);
#else
                using (JsonDocument document = JsonDocument.Parse(RequireApproval))
                {
                    JsonSerializer.Serialize(writer, document.RootElement);
                }
#endif
            }
        }

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

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

        internal static InternalMCPTool DeserializeInternalMCPTool(JsonElement element, ModelReaderWriterOptions options)
        {
            if (element.ValueKind == JsonValueKind.Null)
            {
                return null;
            }
            InternalToolType kind = default;
            IDictionary<string, BinaryData> additionalBinaryDataProperties = new ChangeTrackingDictionary<string, BinaryData>();
            string serverLabel = default;
            string serverUrl = default;
            IDictionary<string, string> headers = default;
            BinaryData allowedTools = default;
            BinaryData requireApproval = default;
            foreach (var prop in element.EnumerateObject())
            {
                if (prop.NameEquals("type"u8))
                {
                    kind = new InternalToolType(prop.Value.GetString());
                    continue;
                }
                if (prop.NameEquals("server_label"u8))
                {
                    serverLabel = prop.Value.GetString();
                    continue;
                }
                if (prop.NameEquals("server_url"u8))
                {
                    serverUrl = prop.Value.GetString();
                    continue;
                }
                if (prop.NameEquals("headers"u8))
                {
                    if (prop.Value.ValueKind == JsonValueKind.Null)
                    {
                        continue;
                    }
                    Dictionary<string, string> dictionary = new Dictionary<string, string>();
                    foreach (var prop0 in prop.Value.EnumerateObject())
                    {
                        if (prop0.Value.ValueKind == JsonValueKind.Null)
                        {
                            dictionary.Add(prop0.Name, null);
                        }
                        else
                        {
                            dictionary.Add(prop0.Name, prop0.Value.GetString());
                        }
                    }
                    headers = dictionary;
                    continue;
                }
                if (prop.NameEquals("allowed_tools"u8))
                {
                    if (prop.Value.ValueKind == JsonValueKind.Null)
                    {
                        allowedTools = null;
                        continue;
                    }
                    allowedTools = BinaryData.FromString(prop.Value.GetRawText());
                    continue;
                }
                if (prop.NameEquals("require_approval"u8))
                {
                    if (prop.Value.ValueKind == JsonValueKind.Null)
                    {
                        requireApproval = null;
                        continue;
                    }
                    requireApproval = BinaryData.FromString(prop.Value.GetRawText());
                    continue;
                }
                // Plugin customization: remove options.Format != "W" check
                additionalBinaryDataProperties.Add(prop.Name, BinaryData.FromString(prop.Value.GetRawText()));
            }
            return new InternalMCPTool(
                kind,
                additionalBinaryDataProperties,
                serverLabel,
                serverUrl,
                headers ?? new ChangeTrackingDictionary<string, string>(),
                allowedTools,
                requireApproval);
        }

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

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

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

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

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