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

        void IJsonModel<InternalResponsesMessageAnnotationFileCitation>.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<InternalResponsesMessageAnnotationFileCitation>)this).GetFormatFromOptions(options) : options.Format;
            if (format != "J")
            {
                throw new FormatException($"The model {nameof(InternalResponsesMessageAnnotationFileCitation)} does not support writing '{format}' format.");
            }
            base.JsonModelWriteCore(writer, options);
            if (_additionalBinaryDataProperties?.ContainsKey("index") != true)
            {
                writer.WritePropertyName("index"u8);
                writer.WriteNumberValue(Index);
            }
            if (_additionalBinaryDataProperties?.ContainsKey("file_id") != true)
            {
                writer.WritePropertyName("file_id"u8);
                writer.WriteStringValue(FileId);
            }
        }

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

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

        internal static InternalResponsesMessageAnnotationFileCitation DeserializeInternalResponsesMessageAnnotationFileCitation(JsonElement element, ModelReaderWriterOptions options)
        {
            if (element.ValueKind == JsonValueKind.Null)
            {
                return null;
            }
            ResponseMessageAnnotationKind kind = default;
            IDictionary<string, BinaryData> additionalBinaryDataProperties = new ChangeTrackingDictionary<string, BinaryData>();
            int index = default;
            string fileId = default;
            foreach (var prop in element.EnumerateObject())
            {
                if (prop.NameEquals("type"u8))
                {
                    kind = prop.Value.GetString().ToResponseMessageAnnotationKind();
                    continue;
                }
                if (prop.NameEquals("index"u8))
                {
                    index = prop.Value.GetInt32();
                    continue;
                }
                if (prop.NameEquals("file_id"u8))
                {
                    fileId = prop.Value.GetString();
                    continue;
                }
                additionalBinaryDataProperties.Add(prop.Name, BinaryData.FromString(prop.Value.GetRawText()));
            }
            return new InternalResponsesMessageAnnotationFileCitation(kind, additionalBinaryDataProperties, index, fileId);
        }

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

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

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

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

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

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

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