// <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.Responses
{
    internal partial class InternalAnnotationUrlCitation : IJsonModel<InternalAnnotationUrlCitation>
    {
        internal InternalAnnotationUrlCitation() : this(ResponseMessageAnnotationKind.UriCitation, null, null, default, default, null)
        {
        }

        void IJsonModel<InternalAnnotationUrlCitation>.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<InternalAnnotationUrlCitation>)this).GetFormatFromOptions(options) : options.Format;
            if (format != "J")
            {
                throw new FormatException($"The model {nameof(InternalAnnotationUrlCitation)} does not support writing '{format}' format.");
            }
            base.JsonModelWriteCore(writer, options);
            if (_additionalBinaryDataProperties?.ContainsKey("url") != true)
            {
                writer.WritePropertyName("url"u8);
                writer.WriteStringValue(Url.AbsoluteUri);
            }
            if (_additionalBinaryDataProperties?.ContainsKey("start_index") != true)
            {
                writer.WritePropertyName("start_index"u8);
                writer.WriteNumberValue(StartIndex);
            }
            if (_additionalBinaryDataProperties?.ContainsKey("end_index") != true)
            {
                writer.WritePropertyName("end_index"u8);
                writer.WriteNumberValue(EndIndex);
            }
            if (_additionalBinaryDataProperties?.ContainsKey("title") != true)
            {
                writer.WritePropertyName("title"u8);
                writer.WriteStringValue(Title);
            }
        }

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

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

        internal static InternalAnnotationUrlCitation DeserializeInternalAnnotationUrlCitation(JsonElement element, ModelReaderWriterOptions options)
        {
            if (element.ValueKind == JsonValueKind.Null)
            {
                return null;
            }
            ResponseMessageAnnotationKind kind = default;
            IDictionary<string, BinaryData> additionalBinaryDataProperties = new ChangeTrackingDictionary<string, BinaryData>();
            Uri url = default;
            int startIndex = default;
            int endIndex = default;
            string title = default;
            foreach (var prop in element.EnumerateObject())
            {
                if (prop.NameEquals("type"u8))
                {
                    kind = prop.Value.GetString().ToResponseMessageAnnotationKind();
                    continue;
                }
                if (prop.NameEquals("url"u8))
                {
                    url = new Uri(prop.Value.GetString());
                    continue;
                }
                if (prop.NameEquals("start_index"u8))
                {
                    startIndex = prop.Value.GetInt32();
                    continue;
                }
                if (prop.NameEquals("end_index"u8))
                {
                    endIndex = prop.Value.GetInt32();
                    continue;
                }
                if (prop.NameEquals("title"u8))
                {
                    title = prop.Value.GetString();
                    continue;
                }
                // Plugin customization: remove options.Format != "W" check
                additionalBinaryDataProperties.Add(prop.Name, BinaryData.FromString(prop.Value.GetRawText()));
            }
            return new InternalAnnotationUrlCitation(
                kind,
                additionalBinaryDataProperties,
                url,
                startIndex,
                endIndex,
                title);
        }

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

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

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

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

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