// <auto-generated/>

#nullable disable

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

namespace OpenAI.Graders
{
    public partial class RunGraderResponseMetadataErrors : IJsonModel<RunGraderResponseMetadataErrors>
    {
        internal RunGraderResponseMetadataErrors()
        {
        }

        void IJsonModel<RunGraderResponseMetadataErrors>.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<RunGraderResponseMetadataErrors>)this).GetFormatFromOptions(options) : options.Format;
            if (format != "J")
            {
                throw new FormatException($"The model {nameof(RunGraderResponseMetadataErrors)} does not support writing '{format}' format.");
            }
            if (_additionalBinaryDataProperties?.ContainsKey("formula_parse_error") != true)
            {
                writer.WritePropertyName("formula_parse_error"u8);
                writer.WriteBooleanValue(FormulaParseError);
            }
            if (_additionalBinaryDataProperties?.ContainsKey("sample_parse_error") != true)
            {
                writer.WritePropertyName("sample_parse_error"u8);
                writer.WriteBooleanValue(SampleParseError);
            }
            if (_additionalBinaryDataProperties?.ContainsKey("truncated_observation_error") != true)
            {
                writer.WritePropertyName("truncated_observation_error"u8);
                writer.WriteBooleanValue(TruncatedObservationError);
            }
            if (_additionalBinaryDataProperties?.ContainsKey("unresponsive_reward_error") != true)
            {
                writer.WritePropertyName("unresponsive_reward_error"u8);
                writer.WriteBooleanValue(UnresponsiveRewardError);
            }
            if (_additionalBinaryDataProperties?.ContainsKey("invalid_variable_error") != true)
            {
                writer.WritePropertyName("invalid_variable_error"u8);
                writer.WriteBooleanValue(InvalidVariableError);
            }
            if (_additionalBinaryDataProperties?.ContainsKey("other_error") != true)
            {
                writer.WritePropertyName("other_error"u8);
                writer.WriteBooleanValue(OtherError);
            }
            if (_additionalBinaryDataProperties?.ContainsKey("python_grader_server_error") != true)
            {
                writer.WritePropertyName("python_grader_server_error"u8);
                writer.WriteBooleanValue(PythonGraderServerError);
            }
            if (_additionalBinaryDataProperties?.ContainsKey("python_grader_server_error_type") != true)
            {
                if (Optional.IsDefined(PythonGraderServerErrorType))
                {
                    writer.WritePropertyName("python_grader_server_error_type"u8);
                    writer.WriteStringValue(PythonGraderServerErrorType);
                }
                else
                {
                    writer.WriteNull("python_grader_server_error_type"u8);
                }
            }
            if (_additionalBinaryDataProperties?.ContainsKey("python_grader_runtime_error") != true)
            {
                writer.WritePropertyName("python_grader_runtime_error"u8);
                writer.WriteBooleanValue(PythonGraderRuntimeError);
            }
            if (_additionalBinaryDataProperties?.ContainsKey("python_grader_runtime_error_details") != true)
            {
                if (Optional.IsDefined(PythonGraderRuntimeErrorDetails))
                {
                    writer.WritePropertyName("python_grader_runtime_error_details"u8);
                    writer.WriteStringValue(PythonGraderRuntimeErrorDetails);
                }
                else
                {
                    writer.WriteNull("python_grader_runtime_error_details"u8);
                }
            }
            if (_additionalBinaryDataProperties?.ContainsKey("model_grader_server_error") != true)
            {
                writer.WritePropertyName("model_grader_server_error"u8);
                writer.WriteBooleanValue(ModelGraderServerError);
            }
            if (_additionalBinaryDataProperties?.ContainsKey("model_grader_refusal_error") != true)
            {
                writer.WritePropertyName("model_grader_refusal_error"u8);
                writer.WriteBooleanValue(ModelGraderRefusalError);
            }
            if (_additionalBinaryDataProperties?.ContainsKey("model_grader_parse_error") != true)
            {
                writer.WritePropertyName("model_grader_parse_error"u8);
                writer.WriteBooleanValue(ModelGraderParseError);
            }
            if (_additionalBinaryDataProperties?.ContainsKey("model_grader_server_error_details") != true)
            {
                if (Optional.IsDefined(ModelGraderServerErrorDetails))
                {
                    writer.WritePropertyName("model_grader_server_error_details"u8);
                    writer.WriteStringValue(ModelGraderServerErrorDetails);
                }
                else
                {
                    writer.WriteNull("model_grader_server_error_details"u8);
                }
            }
            // 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
                }
            }
        }

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

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

        internal static RunGraderResponseMetadataErrors DeserializeRunGraderResponseMetadataErrors(JsonElement element, ModelReaderWriterOptions options)
        {
            if (element.ValueKind == JsonValueKind.Null)
            {
                return null;
            }
            bool formulaParseError = default;
            bool sampleParseError = default;
            bool truncatedObservationError = default;
            bool unresponsiveRewardError = default;
            bool invalidVariableError = default;
            bool otherError = default;
            bool pythonGraderServerError = default;
            string pythonGraderServerErrorType = default;
            bool pythonGraderRuntimeError = default;
            string pythonGraderRuntimeErrorDetails = default;
            bool modelGraderServerError = default;
            bool modelGraderRefusalError = default;
            bool modelGraderParseError = default;
            string modelGraderServerErrorDetails = default;
            IDictionary<string, BinaryData> additionalBinaryDataProperties = new ChangeTrackingDictionary<string, BinaryData>();
            foreach (var prop in element.EnumerateObject())
            {
                if (prop.NameEquals("formula_parse_error"u8))
                {
                    formulaParseError = prop.Value.GetBoolean();
                    continue;
                }
                if (prop.NameEquals("sample_parse_error"u8))
                {
                    sampleParseError = prop.Value.GetBoolean();
                    continue;
                }
                if (prop.NameEquals("truncated_observation_error"u8))
                {
                    truncatedObservationError = prop.Value.GetBoolean();
                    continue;
                }
                if (prop.NameEquals("unresponsive_reward_error"u8))
                {
                    unresponsiveRewardError = prop.Value.GetBoolean();
                    continue;
                }
                if (prop.NameEquals("invalid_variable_error"u8))
                {
                    invalidVariableError = prop.Value.GetBoolean();
                    continue;
                }
                if (prop.NameEquals("other_error"u8))
                {
                    otherError = prop.Value.GetBoolean();
                    continue;
                }
                if (prop.NameEquals("python_grader_server_error"u8))
                {
                    pythonGraderServerError = prop.Value.GetBoolean();
                    continue;
                }
                if (prop.NameEquals("python_grader_server_error_type"u8))
                {
                    if (prop.Value.ValueKind == JsonValueKind.Null)
                    {
                        pythonGraderServerErrorType = null;
                        continue;
                    }
                    pythonGraderServerErrorType = prop.Value.GetString();
                    continue;
                }
                if (prop.NameEquals("python_grader_runtime_error"u8))
                {
                    pythonGraderRuntimeError = prop.Value.GetBoolean();
                    continue;
                }
                if (prop.NameEquals("python_grader_runtime_error_details"u8))
                {
                    if (prop.Value.ValueKind == JsonValueKind.Null)
                    {
                        pythonGraderRuntimeErrorDetails = null;
                        continue;
                    }
                    pythonGraderRuntimeErrorDetails = prop.Value.GetString();
                    continue;
                }
                if (prop.NameEquals("model_grader_server_error"u8))
                {
                    modelGraderServerError = prop.Value.GetBoolean();
                    continue;
                }
                if (prop.NameEquals("model_grader_refusal_error"u8))
                {
                    modelGraderRefusalError = prop.Value.GetBoolean();
                    continue;
                }
                if (prop.NameEquals("model_grader_parse_error"u8))
                {
                    modelGraderParseError = prop.Value.GetBoolean();
                    continue;
                }
                if (prop.NameEquals("model_grader_server_error_details"u8))
                {
                    if (prop.Value.ValueKind == JsonValueKind.Null)
                    {
                        modelGraderServerErrorDetails = null;
                        continue;
                    }
                    modelGraderServerErrorDetails = prop.Value.GetString();
                    continue;
                }
                // Plugin customization: remove options.Format != "W" check
                additionalBinaryDataProperties.Add(prop.Name, BinaryData.FromString(prop.Value.GetRawText()));
            }
            return new RunGraderResponseMetadataErrors(
                formulaParseError,
                sampleParseError,
                truncatedObservationError,
                unresponsiveRewardError,
                invalidVariableError,
                otherError,
                pythonGraderServerError,
                pythonGraderServerErrorType,
                pythonGraderRuntimeError,
                pythonGraderRuntimeErrorDetails,
                modelGraderServerError,
                modelGraderRefusalError,
                modelGraderParseError,
                modelGraderServerErrorDetails,
                additionalBinaryDataProperties);
        }

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

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

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

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

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