// <auto-generated/>

#nullable disable

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

namespace OpenAI.FineTuning
{
    internal partial class InternalFineTuneSupervisedMethodHyperparameters : IJsonModel<InternalFineTuneSupervisedMethodHyperparameters>
    {
        void IJsonModel<InternalFineTuneSupervisedMethodHyperparameters>.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<InternalFineTuneSupervisedMethodHyperparameters>)this).GetFormatFromOptions(options) : options.Format;
            if (format != "J")
            {
                throw new FormatException($"The model {nameof(InternalFineTuneSupervisedMethodHyperparameters)} does not support writing '{format}' format.");
            }
            if (Optional.IsDefined(BatchSize) && _additionalBinaryDataProperties?.ContainsKey("batch_size") != true)
            {
                writer.WritePropertyName("batch_size"u8);
#if NET6_0_OR_GREATER
                writer.WriteRawValue(BatchSize);
#else
                using (JsonDocument document = JsonDocument.Parse(BatchSize))
                {
                    JsonSerializer.Serialize(writer, document.RootElement);
                }
#endif
            }
            if (Optional.IsDefined(LearningRateMultiplier) && _additionalBinaryDataProperties?.ContainsKey("learning_rate_multiplier") != true)
            {
                writer.WritePropertyName("learning_rate_multiplier"u8);
#if NET6_0_OR_GREATER
                writer.WriteRawValue(LearningRateMultiplier);
#else
                using (JsonDocument document = JsonDocument.Parse(LearningRateMultiplier))
                {
                    JsonSerializer.Serialize(writer, document.RootElement);
                }
#endif
            }
            if (Optional.IsDefined(NEpochs) && _additionalBinaryDataProperties?.ContainsKey("n_epochs") != true)
            {
                writer.WritePropertyName("n_epochs"u8);
#if NET6_0_OR_GREATER
                writer.WriteRawValue(NEpochs);
#else
                using (JsonDocument document = JsonDocument.Parse(NEpochs))
                {
                    JsonSerializer.Serialize(writer, document.RootElement);
                }
#endif
            }
            if (true && _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
                }
            }
        }

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

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

        internal static InternalFineTuneSupervisedMethodHyperparameters DeserializeInternalFineTuneSupervisedMethodHyperparameters(JsonElement element, ModelReaderWriterOptions options)
        {
            if (element.ValueKind == JsonValueKind.Null)
            {
                return null;
            }
            BinaryData batchSize = default;
            BinaryData learningRateMultiplier = default;
            BinaryData nEpochs = default;
            IDictionary<string, BinaryData> additionalBinaryDataProperties = new ChangeTrackingDictionary<string, BinaryData>();
            foreach (var prop in element.EnumerateObject())
            {
                if (prop.NameEquals("batch_size"u8))
                {
                    if (prop.Value.ValueKind == JsonValueKind.Null)
                    {
                        continue;
                    }
                    batchSize = BinaryData.FromString(prop.Value.GetRawText());
                    continue;
                }
                if (prop.NameEquals("learning_rate_multiplier"u8))
                {
                    if (prop.Value.ValueKind == JsonValueKind.Null)
                    {
                        continue;
                    }
                    learningRateMultiplier = BinaryData.FromString(prop.Value.GetRawText());
                    continue;
                }
                if (prop.NameEquals("n_epochs"u8))
                {
                    if (prop.Value.ValueKind == JsonValueKind.Null)
                    {
                        continue;
                    }
                    nEpochs = BinaryData.FromString(prop.Value.GetRawText());
                    continue;
                }
                if (true)
                {
                    additionalBinaryDataProperties.Add(prop.Name, BinaryData.FromString(prop.Value.GetRawText()));
                }
            }
            return new InternalFineTuneSupervisedMethodHyperparameters(batchSize, learningRateMultiplier, nEpochs, additionalBinaryDataProperties);
        }

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

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

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

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

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

        public static implicit operator BinaryContent(InternalFineTuneSupervisedMethodHyperparameters internalFineTuneSupervisedMethodHyperparameters)
        {
            if (internalFineTuneSupervisedMethodHyperparameters == null)
            {
                return null;
            }
            return BinaryContent.Create(internalFineTuneSupervisedMethodHyperparameters, ModelSerializationExtensions.WireOptions);
        }

        public static explicit operator InternalFineTuneSupervisedMethodHyperparameters(ClientResult result)
        {
            using PipelineResponse response = result.GetRawResponse();
            using JsonDocument document = JsonDocument.Parse(response.Content);
            return DeserializeInternalFineTuneSupervisedMethodHyperparameters(document.RootElement, ModelSerializationExtensions.WireOptions);
        }
    }
}
