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

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

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

        internal static InternalItemContentInputFile DeserializeInternalItemContentInputFile(JsonElement element, ModelReaderWriterOptions options)
        {
            if (element.ValueKind == JsonValueKind.Null)
            {
                return null;
            }
            InternalItemContentType internalType = default;
            IDictionary<string, BinaryData> additionalBinaryDataProperties = new ChangeTrackingDictionary<string, BinaryData>();
            string fileId = default;
            string filename = default;
            string internalFileData = default;
            foreach (var prop in element.EnumerateObject())
            {
                if (prop.NameEquals("type"u8))
                {
                    internalType = new InternalItemContentType(prop.Value.GetString());
                    continue;
                }
                if (prop.NameEquals("file_id"u8))
                {
                    if (prop.Value.ValueKind == JsonValueKind.Null)
                    {
                        fileId = null;
                        continue;
                    }
                    fileId = prop.Value.GetString();
                    continue;
                }
                if (prop.NameEquals("filename"u8))
                {
                    filename = prop.Value.GetString();
                    continue;
                }
                if (prop.NameEquals("file_data"u8))
                {
                    internalFileData = prop.Value.GetString();
                    continue;
                }
                // Plugin customization: remove options.Format != "W" check
                additionalBinaryDataProperties.Add(prop.Name, BinaryData.FromString(prop.Value.GetRawText()));
            }
            return new InternalItemContentInputFile(internalType, additionalBinaryDataProperties, fileId, filename, internalFileData);
        }

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

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

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

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

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