// <auto-generated/>

#nullable disable

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

namespace OpenAI.Chat
{
    internal partial class InternalChatOutputPredictionContent : IJsonModel<InternalChatOutputPredictionContent>
    {
        internal InternalChatOutputPredictionContent() : this(InternalChatOutputPredictionKind.StaticContent, null, null)
        {
        }

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

        protected override void JsonModelWriteCore(Utf8JsonWriter writer, ModelReaderWriterOptions options)
        {
            string format = options.Format == "W" ? ((IPersistableModel<InternalChatOutputPredictionContent>)this).GetFormatFromOptions(options) : options.Format;
            if (format != "J")
            {
                throw new FormatException($"The model {nameof(InternalChatOutputPredictionContent)} does not support writing '{format}' format.");
            }
            base.JsonModelWriteCore(writer, options);
            if (_additionalBinaryDataProperties?.ContainsKey("content") != true)
            {
                writer.WritePropertyName("content"u8);
                SerializeContentValue(writer, options);
            }
        }

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

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

        internal static InternalChatOutputPredictionContent DeserializeInternalChatOutputPredictionContent(JsonElement element, ModelReaderWriterOptions options)
        {
            if (element.ValueKind == JsonValueKind.Null)
            {
                return null;
            }
            InternalChatOutputPredictionKind kind = default;
            IDictionary<string, BinaryData> additionalBinaryDataProperties = new ChangeTrackingDictionary<string, BinaryData>();
            ChatMessageContent content = default;
            foreach (var prop in element.EnumerateObject())
            {
                if (prop.NameEquals("type"u8))
                {
                    kind = new InternalChatOutputPredictionKind(prop.Value.GetString());
                    continue;
                }
                if (prop.NameEquals("content"u8))
                {
                    DeserializeContentValue(prop, ref content);
                    continue;
                }
                // Plugin customization: remove options.Format != "W" check
                additionalBinaryDataProperties.Add(prop.Name, BinaryData.FromString(prop.Value.GetRawText()));
            }
            return new InternalChatOutputPredictionContent(kind, additionalBinaryDataProperties, content);
        }

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

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

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

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

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