// <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 InternalResponsesTextFormatJsonSchema : IJsonModel<InternalResponsesTextFormatJsonSchema>
    {
        internal InternalResponsesTextFormatJsonSchema()
        {
        }

        void IJsonModel<InternalResponsesTextFormatJsonSchema>.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<InternalResponsesTextFormatJsonSchema>)this).GetFormatFromOptions(options) : options.Format;
            if (format != "J")
            {
                throw new FormatException($"The model {nameof(InternalResponsesTextFormatJsonSchema)} does not support writing '{format}' format.");
            }
            base.JsonModelWriteCore(writer, options);
            if (_additionalBinaryDataProperties?.ContainsKey("name") != true)
            {
                writer.WritePropertyName("name"u8);
                writer.WriteStringValue(Name);
            }
            if (_additionalBinaryDataProperties?.ContainsKey("schema") != true)
            {
                writer.WritePropertyName("schema"u8);
#if NET6_0_OR_GREATER
                writer.WriteRawValue(Schema);
#else
                using (JsonDocument document = JsonDocument.Parse(Schema))
                {
                    JsonSerializer.Serialize(writer, document.RootElement);
                }
#endif
            }
            if (Optional.IsDefined(Description) && _additionalBinaryDataProperties?.ContainsKey("description") != true)
            {
                writer.WritePropertyName("description"u8);
                writer.WriteStringValue(Description);
            }
            if (Optional.IsDefined(Strict) && _additionalBinaryDataProperties?.ContainsKey("strict") != true)
            {
                writer.WritePropertyName("strict"u8);
                writer.WriteBooleanValue(Strict.Value);
            }
        }

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

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

        internal static InternalResponsesTextFormatJsonSchema DeserializeInternalResponsesTextFormatJsonSchema(JsonElement element, ModelReaderWriterOptions options)
        {
            if (element.ValueKind == JsonValueKind.Null)
            {
                return null;
            }
            InternalResponsesTextFormatType internalType = default;
            IDictionary<string, BinaryData> additionalBinaryDataProperties = new ChangeTrackingDictionary<string, BinaryData>();
            string name = default;
            BinaryData schema = default;
            string description = default;
            bool? strict = default;
            foreach (var prop in element.EnumerateObject())
            {
                if (prop.NameEquals("type"u8))
                {
                    internalType = new InternalResponsesTextFormatType(prop.Value.GetString());
                    continue;
                }
                if (prop.NameEquals("name"u8))
                {
                    name = prop.Value.GetString();
                    continue;
                }
                if (prop.NameEquals("schema"u8))
                {
                    schema = BinaryData.FromString(prop.Value.GetRawText());
                    continue;
                }
                if (prop.NameEquals("description"u8))
                {
                    description = prop.Value.GetString();
                    continue;
                }
                if (prop.NameEquals("strict"u8))
                {
                    if (prop.Value.ValueKind == JsonValueKind.Null)
                    {
                        strict = null;
                        continue;
                    }
                    strict = prop.Value.GetBoolean();
                    continue;
                }
                additionalBinaryDataProperties.Add(prop.Name, BinaryData.FromString(prop.Value.GetRawText()));
            }
            return new InternalResponsesTextFormatJsonSchema(
                internalType,
                additionalBinaryDataProperties,
                name,
                schema,
                description,
                strict);
        }

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

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

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

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

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

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

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