// <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.Chat
{
    internal partial class InternalChatOutputPredictionContent : IJsonModel<InternalChatOutputPredictionContent>
    {
        internal InternalChatOutputPredictionContent()
        {
        }

        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 @type = default;
            IDictionary<string, BinaryData> additionalBinaryDataProperties = new ChangeTrackingDictionary<string, BinaryData>();
            ChatMessageContent content = default;
            foreach (var prop in element.EnumerateObject())
            {
                if (prop.NameEquals("type"u8))
                {
                    @type = new InternalChatOutputPredictionKind(prop.Value.GetString());
                    continue;
                }
                if (prop.NameEquals("content"u8))
                {
                    DeserializeContentValue(prop, ref content);
                    continue;
                }
                additionalBinaryDataProperties.Add(prop.Name, BinaryData.FromString(prop.Value.GetRawText()));
            }
            return new InternalChatOutputPredictionContent(@type, 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);
                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";

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

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