// <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 InternalEvalJsonlRunDataSourceParams : IJsonModel<InternalEvalJsonlRunDataSourceParams>
    {
        internal InternalEvalJsonlRunDataSourceParams() : this(InternalEvalRunDataSourceType.Jsonl, null, null)
        {
        }

        void IJsonModel<InternalEvalJsonlRunDataSourceParams>.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<InternalEvalJsonlRunDataSourceParams>)this).GetFormatFromOptions(options) : options.Format;
            if (format != "J")
            {
                throw new FormatException($"The model {nameof(InternalEvalJsonlRunDataSourceParams)} does not support writing '{format}' format.");
            }
            base.JsonModelWriteCore(writer, options);
            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
            }
        }

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

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

        internal static InternalEvalJsonlRunDataSourceParams DeserializeInternalEvalJsonlRunDataSourceParams(JsonElement element, ModelReaderWriterOptions options)
        {
            if (element.ValueKind == JsonValueKind.Null)
            {
                return null;
            }
            InternalEvalRunDataSourceType kind = default;
            IDictionary<string, BinaryData> additionalBinaryDataProperties = new ChangeTrackingDictionary<string, BinaryData>();
            BinaryData source = default;
            foreach (var prop in element.EnumerateObject())
            {
                if (prop.NameEquals("type"u8))
                {
                    kind = new InternalEvalRunDataSourceType(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 InternalEvalJsonlRunDataSourceParams(kind, additionalBinaryDataProperties, source);
        }

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

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

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

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

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