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

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

        protected virtual void JsonModelWriteCore(Utf8JsonWriter writer, ModelReaderWriterOptions options)
        {
            string format = options.Format == "W" ? ((IPersistableModel<InternalLocalShellExecAction>)this).GetFormatFromOptions(options) : options.Format;
            if (format != "J")
            {
                throw new FormatException($"The model {nameof(InternalLocalShellExecAction)} does not support writing '{format}' format.");
            }
            if (_additionalBinaryDataProperties?.ContainsKey("type") != true)
            {
                writer.WritePropertyName("type"u8);
                writer.WriteStringValue(Kind);
            }
            if (_additionalBinaryDataProperties?.ContainsKey("command") != true)
            {
                writer.WritePropertyName("command"u8);
                writer.WriteStartArray();
                foreach (string item in Command)
                {
                    if (item == null)
                    {
                        writer.WriteNullValue();
                        continue;
                    }
                    writer.WriteStringValue(item);
                }
                writer.WriteEndArray();
            }
            if (Optional.IsDefined(TimeoutMs) && _additionalBinaryDataProperties?.ContainsKey("timeout_ms") != true)
            {
                writer.WritePropertyName("timeout_ms"u8);
                writer.WriteNumberValue(TimeoutMs.Value);
            }
            if (Optional.IsDefined(WorkingDirectory) && _additionalBinaryDataProperties?.ContainsKey("working_directory") != true)
            {
                writer.WritePropertyName("working_directory"u8);
                writer.WriteStringValue(WorkingDirectory);
            }
            if (_additionalBinaryDataProperties?.ContainsKey("env") != true)
            {
                writer.WritePropertyName("env"u8);
                writer.WriteStartObject();
                foreach (var item in Env)
                {
                    writer.WritePropertyName(item.Key);
                    if (item.Value == null)
                    {
                        writer.WriteNullValue();
                        continue;
                    }
                    writer.WriteStringValue(item.Value);
                }
                writer.WriteEndObject();
            }
            if (Optional.IsDefined(User) && _additionalBinaryDataProperties?.ContainsKey("user") != true)
            {
                writer.WritePropertyName("user"u8);
                writer.WriteStringValue(User);
            }
            // Plugin customization: remove options.Format != "W" check
            if (_additionalBinaryDataProperties != null)
            {
                foreach (var item in _additionalBinaryDataProperties)
                {
                    if (ModelSerializationExtensions.IsSentinelValue(item.Value))
                    {
                        continue;
                    }
                    writer.WritePropertyName(item.Key);
#if NET6_0_OR_GREATER
                    writer.WriteRawValue(item.Value);
#else
                    using (JsonDocument document = JsonDocument.Parse(item.Value))
                    {
                        JsonSerializer.Serialize(writer, document.RootElement);
                    }
#endif
                }
            }
        }

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

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

        internal static InternalLocalShellExecAction DeserializeInternalLocalShellExecAction(JsonElement element, ModelReaderWriterOptions options)
        {
            if (element.ValueKind == JsonValueKind.Null)
            {
                return null;
            }
            string kind = default;
            IList<string> command = default;
            int? timeoutMs = default;
            string workingDirectory = default;
            IDictionary<string, string> env = default;
            string user = default;
            IDictionary<string, BinaryData> additionalBinaryDataProperties = new ChangeTrackingDictionary<string, BinaryData>();
            foreach (var prop in element.EnumerateObject())
            {
                if (prop.NameEquals("type"u8))
                {
                    kind = prop.Value.GetString();
                    continue;
                }
                if (prop.NameEquals("command"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());
                        }
                    }
                    command = array;
                    continue;
                }
                if (prop.NameEquals("timeout_ms"u8))
                {
                    if (prop.Value.ValueKind == JsonValueKind.Null)
                    {
                        timeoutMs = null;
                        continue;
                    }
                    timeoutMs = prop.Value.GetInt32();
                    continue;
                }
                if (prop.NameEquals("working_directory"u8))
                {
                    if (prop.Value.ValueKind == JsonValueKind.Null)
                    {
                        workingDirectory = null;
                        continue;
                    }
                    workingDirectory = prop.Value.GetString();
                    continue;
                }
                if (prop.NameEquals("env"u8))
                {
                    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());
                        }
                    }
                    env = dictionary;
                    continue;
                }
                if (prop.NameEquals("user"u8))
                {
                    if (prop.Value.ValueKind == JsonValueKind.Null)
                    {
                        user = null;
                        continue;
                    }
                    user = prop.Value.GetString();
                    continue;
                }
                // Plugin customization: remove options.Format != "W" check
                additionalBinaryDataProperties.Add(prop.Name, BinaryData.FromString(prop.Value.GetRawText()));
            }
            return new InternalLocalShellExecAction(
                kind,
                command,
                timeoutMs,
                workingDirectory,
                env,
                user,
                additionalBinaryDataProperties);
        }

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

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

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

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

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