// <auto-generated/>

#nullable disable

using System;
using System.ClientModel.Primitives;
using System.Collections.Generic;
using System.Text.Json;
using OpenAI;

namespace OpenAI.Moderations
{
    internal partial class InternalModerationCategories : IJsonModel<InternalModerationCategories>
    {
        internal InternalModerationCategories()
        {
        }

        void IJsonModel<InternalModerationCategories>.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<InternalModerationCategories>)this).GetFormatFromOptions(options) : options.Format;
            if (format != "J")
            {
                throw new FormatException($"The model {nameof(InternalModerationCategories)} does not support writing '{format}' format.");
            }
            if (_additionalBinaryDataProperties?.ContainsKey("hate") != true)
            {
                writer.WritePropertyName("hate"u8);
                writer.WriteBooleanValue(Hate);
            }
            if (_additionalBinaryDataProperties?.ContainsKey("hate/threatening") != true)
            {
                writer.WritePropertyName("hate/threatening"u8);
                writer.WriteBooleanValue(HateThreatening);
            }
            if (_additionalBinaryDataProperties?.ContainsKey("harassment") != true)
            {
                writer.WritePropertyName("harassment"u8);
                writer.WriteBooleanValue(Harassment);
            }
            if (_additionalBinaryDataProperties?.ContainsKey("harassment/threatening") != true)
            {
                writer.WritePropertyName("harassment/threatening"u8);
                writer.WriteBooleanValue(HarassmentThreatening);
            }
            if (_additionalBinaryDataProperties?.ContainsKey("illicit") != true)
            {
                writer.WritePropertyName("illicit"u8);
                writer.WriteBooleanValue(Illicit);
            }
            if (_additionalBinaryDataProperties?.ContainsKey("illicit/violent") != true)
            {
                writer.WritePropertyName("illicit/violent"u8);
                writer.WriteBooleanValue(IllicitViolent);
            }
            if (_additionalBinaryDataProperties?.ContainsKey("self-harm") != true)
            {
                writer.WritePropertyName("self-harm"u8);
                writer.WriteBooleanValue(SelfHarm);
            }
            if (_additionalBinaryDataProperties?.ContainsKey("self-harm/intent") != true)
            {
                writer.WritePropertyName("self-harm/intent"u8);
                writer.WriteBooleanValue(SelfHarmIntent);
            }
            if (_additionalBinaryDataProperties?.ContainsKey("self-harm/instructions") != true)
            {
                writer.WritePropertyName("self-harm/instructions"u8);
                writer.WriteBooleanValue(SelfHarmInstructions);
            }
            if (_additionalBinaryDataProperties?.ContainsKey("sexual") != true)
            {
                writer.WritePropertyName("sexual"u8);
                writer.WriteBooleanValue(Sexual);
            }
            if (_additionalBinaryDataProperties?.ContainsKey("sexual/minors") != true)
            {
                writer.WritePropertyName("sexual/minors"u8);
                writer.WriteBooleanValue(SexualMinors);
            }
            if (_additionalBinaryDataProperties?.ContainsKey("violence") != true)
            {
                writer.WritePropertyName("violence"u8);
                writer.WriteBooleanValue(Violence);
            }
            if (_additionalBinaryDataProperties?.ContainsKey("violence/graphic") != true)
            {
                writer.WritePropertyName("violence/graphic"u8);
                writer.WriteBooleanValue(ViolenceGraphic);
            }
            // 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
                }
            }
        }

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

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

        internal static InternalModerationCategories DeserializeInternalModerationCategories(JsonElement element, ModelReaderWriterOptions options)
        {
            if (element.ValueKind == JsonValueKind.Null)
            {
                return null;
            }
            bool hate = default;
            bool hateThreatening = default;
            bool harassment = default;
            bool harassmentThreatening = default;
            bool illicit = default;
            bool illicitViolent = default;
            bool selfHarm = default;
            bool selfHarmIntent = default;
            bool selfHarmInstructions = default;
            bool sexual = default;
            bool sexualMinors = default;
            bool violence = default;
            bool violenceGraphic = default;
            IDictionary<string, BinaryData> additionalBinaryDataProperties = new ChangeTrackingDictionary<string, BinaryData>();
            foreach (var prop in element.EnumerateObject())
            {
                if (prop.NameEquals("hate"u8))
                {
                    hate = prop.Value.GetBoolean();
                    continue;
                }
                if (prop.NameEquals("hate/threatening"u8))
                {
                    hateThreatening = prop.Value.GetBoolean();
                    continue;
                }
                if (prop.NameEquals("harassment"u8))
                {
                    harassment = prop.Value.GetBoolean();
                    continue;
                }
                if (prop.NameEquals("harassment/threatening"u8))
                {
                    harassmentThreatening = prop.Value.GetBoolean();
                    continue;
                }
                if (prop.NameEquals("illicit"u8))
                {
                    illicit = prop.Value.GetBoolean();
                    continue;
                }
                if (prop.NameEquals("illicit/violent"u8))
                {
                    illicitViolent = prop.Value.GetBoolean();
                    continue;
                }
                if (prop.NameEquals("self-harm"u8))
                {
                    selfHarm = prop.Value.GetBoolean();
                    continue;
                }
                if (prop.NameEquals("self-harm/intent"u8))
                {
                    selfHarmIntent = prop.Value.GetBoolean();
                    continue;
                }
                if (prop.NameEquals("self-harm/instructions"u8))
                {
                    selfHarmInstructions = prop.Value.GetBoolean();
                    continue;
                }
                if (prop.NameEquals("sexual"u8))
                {
                    sexual = prop.Value.GetBoolean();
                    continue;
                }
                if (prop.NameEquals("sexual/minors"u8))
                {
                    sexualMinors = prop.Value.GetBoolean();
                    continue;
                }
                if (prop.NameEquals("violence"u8))
                {
                    violence = prop.Value.GetBoolean();
                    continue;
                }
                if (prop.NameEquals("violence/graphic"u8))
                {
                    violenceGraphic = prop.Value.GetBoolean();
                    continue;
                }
                // Plugin customization: remove options.Format != "W" check
                additionalBinaryDataProperties.Add(prop.Name, BinaryData.FromString(prop.Value.GetRawText()));
            }
            return new InternalModerationCategories(
                hate,
                hateThreatening,
                harassment,
                harassmentThreatening,
                illicit,
                illicitViolent,
                selfHarm,
                selfHarmIntent,
                selfHarmInstructions,
                sexual,
                sexualMinors,
                violence,
                violenceGraphic,
                additionalBinaryDataProperties);
        }

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

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

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

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

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