// <auto-generated/>

#nullable disable

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

namespace OpenAI.Evals
{
    internal partial class InternalEvalCompletionsRunDataSourceParams : IJsonModel<InternalEvalCompletionsRunDataSourceParams>
    {
        internal InternalEvalCompletionsRunDataSourceParams() : this(InternalEvalRunDataSourceType.Completions, null, null, null, null, null)
        {
        }

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

        [Experimental("OPENAI001")]
        protected override void JsonModelWriteCore(Utf8JsonWriter writer, ModelReaderWriterOptions options)
        {
            string format = options.Format == "W" ? ((IPersistableModel<InternalEvalCompletionsRunDataSourceParams>)this).GetFormatFromOptions(options) : options.Format;
            if (format != "J")
            {
                throw new FormatException($"The model {nameof(InternalEvalCompletionsRunDataSourceParams)} does not support writing '{format}' format.");
            }
            base.JsonModelWriteCore(writer, options);
            if (Optional.IsDefined(InputMessages) && _additionalBinaryDataProperties?.ContainsKey("input_messages") != true)
            {
                writer.WritePropertyName("input_messages"u8);
#if NET6_0_OR_GREATER
                writer.WriteRawValue(InputMessages);
#else
                using (JsonDocument document = JsonDocument.Parse(InputMessages))
                {
                    JsonSerializer.Serialize(writer, document.RootElement);
                }
#endif
            }
            if (Optional.IsDefined(SamplingParams) && _additionalBinaryDataProperties?.ContainsKey("sampling_params") != true)
            {
                writer.WritePropertyName("sampling_params"u8);
                writer.WriteObjectValue(SamplingParams, options);
            }
            if (Optional.IsDefined(Model) && _additionalBinaryDataProperties?.ContainsKey("model") != true)
            {
                writer.WritePropertyName("model"u8);
                writer.WriteStringValue(Model);
            }
            if (_additionalBinaryDataProperties?.ContainsKey("source") != true)
            {
                writer.WritePropertyName("source"u8);
#if NET6_0_OR_GREATER
                writer.WriteRawValue(Source);
#else
                using (JsonDocument document = JsonDocument.Parse(Source))
                {
                    JsonSerializer.Serialize(writer, document.RootElement);
                }
#endif
            }
        }

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

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

        internal static InternalEvalCompletionsRunDataSourceParams DeserializeInternalEvalCompletionsRunDataSourceParams(JsonElement element, ModelReaderWriterOptions options)
        {
            if (element.ValueKind == JsonValueKind.Null)
            {
                return null;
            }
            InternalEvalRunDataSourceType kind = default;
            IDictionary<string, BinaryData> additionalBinaryDataProperties = new ChangeTrackingDictionary<string, BinaryData>();
            BinaryData inputMessages = default;
            InternalEvalCompletionsRunDataSourceParamsSamplingParams samplingParams = default;
            string model = default;
            BinaryData source = default;
            foreach (var prop in element.EnumerateObject())
            {
                if (prop.NameEquals("type"u8))
                {
                    kind = new InternalEvalRunDataSourceType(prop.Value.GetString());
                    continue;
                }
                if (prop.NameEquals("input_messages"u8))
                {
                    if (prop.Value.ValueKind == JsonValueKind.Null)
                    {
                        continue;
                    }
                    inputMessages = BinaryData.FromString(prop.Value.GetRawText());
                    continue;
                }
                if (prop.NameEquals("sampling_params"u8))
                {
                    if (prop.Value.ValueKind == JsonValueKind.Null)
                    {
                        continue;
                    }
                    samplingParams = InternalEvalCompletionsRunDataSourceParamsSamplingParams.DeserializeInternalEvalCompletionsRunDataSourceParamsSamplingParams(prop.Value, options);
                    continue;
                }
                if (prop.NameEquals("model"u8))
                {
                    model = prop.Value.GetString();
                    continue;
                }
                if (prop.NameEquals("source"u8))
                {
                    source = BinaryData.FromString(prop.Value.GetRawText());
                    continue;
                }
                // Plugin customization: remove options.Format != "W" check
                additionalBinaryDataProperties.Add(prop.Name, BinaryData.FromString(prop.Value.GetRawText()));
            }
            return new InternalEvalCompletionsRunDataSourceParams(
                kind,
                additionalBinaryDataProperties,
                inputMessages,
                samplingParams,
                model,
                source);
        }

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

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

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

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

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