// File generated from our OpenAPI spec by Stainless.

package com.openai.models.beta.threads.messages

import com.fasterxml.jackson.core.JsonGenerator
import com.fasterxml.jackson.core.ObjectCodec
import com.fasterxml.jackson.databind.JsonNode
import com.fasterxml.jackson.databind.SerializerProvider
import com.fasterxml.jackson.databind.annotation.JsonDeserialize
import com.fasterxml.jackson.databind.annotation.JsonSerialize
import com.fasterxml.jackson.module.kotlin.jacksonTypeRef
import com.openai.core.BaseDeserializer
import com.openai.core.BaseSerializer
import com.openai.core.JsonValue
import com.openai.core.getOrThrow
import com.openai.errors.OpenAIInvalidDataException
import java.util.Objects
import java.util.Optional
import kotlin.jvm.optionals.getOrNull

/**
 * A citation within the message that points to a specific quote from a specific File associated
 * with the assistant or the message. Generated when the assistant uses the "file_search" tool to
 * search files.
 */
@JsonDeserialize(using = Annotation.Deserializer::class)
@JsonSerialize(using = Annotation.Serializer::class)
class Annotation
private constructor(
    private val fileCitation: FileCitationAnnotation? = null,
    private val filePath: FilePathAnnotation? = null,
    private val _json: JsonValue? = null,
) {

    /**
     * A citation within the message that points to a specific quote from a specific File associated
     * with the assistant or the message. Generated when the assistant uses the "file_search" tool
     * to search files.
     */
    fun fileCitation(): Optional<FileCitationAnnotation> = Optional.ofNullable(fileCitation)

    /**
     * A URL for the file that's generated when the assistant used the `code_interpreter` tool to
     * generate a file.
     */
    fun filePath(): Optional<FilePathAnnotation> = Optional.ofNullable(filePath)

    fun isFileCitation(): Boolean = fileCitation != null

    fun isFilePath(): Boolean = filePath != null

    /**
     * A citation within the message that points to a specific quote from a specific File associated
     * with the assistant or the message. Generated when the assistant uses the "file_search" tool
     * to search files.
     */
    fun asFileCitation(): FileCitationAnnotation = fileCitation.getOrThrow("fileCitation")

    /**
     * A URL for the file that's generated when the assistant used the `code_interpreter` tool to
     * generate a file.
     */
    fun asFilePath(): FilePathAnnotation = filePath.getOrThrow("filePath")

    fun _json(): Optional<JsonValue> = Optional.ofNullable(_json)

    /**
     * Maps this instance's current variant to a value of type [T] using the given [visitor].
     *
     * Note that this method is _not_ forwards compatible with new variants from the API, unless
     * [visitor] overrides [Visitor.unknown]. To handle variants not known to this version of the
     * SDK gracefully, consider overriding [Visitor.unknown]:
     * ```java
     * import com.openai.core.JsonValue;
     * import java.util.Optional;
     *
     * Optional<String> result = annotation.accept(new Annotation.Visitor<Optional<String>>() {
     *     @Override
     *     public Optional<String> visitFileCitation(FileCitationAnnotation fileCitation) {
     *         return Optional.of(fileCitation.toString());
     *     }
     *
     *     // ...
     *
     *     @Override
     *     public Optional<String> unknown(JsonValue json) {
     *         // Or inspect the `json`.
     *         return Optional.empty();
     *     }
     * });
     * ```
     *
     * @throws OpenAIInvalidDataException if [Visitor.unknown] is not overridden in [visitor] and
     *   the current variant is unknown.
     */
    fun <T> accept(visitor: Visitor<T>): T =
        when {
            fileCitation != null -> visitor.visitFileCitation(fileCitation)
            filePath != null -> visitor.visitFilePath(filePath)
            else -> visitor.unknown(_json)
        }

    private var validated: Boolean = false

    /**
     * Validates that the types of all values in this object match their expected types recursively.
     *
     * This method is _not_ forwards compatible with new types from the API for existing fields.
     *
     * @throws OpenAIInvalidDataException if any value type in this object doesn't match its
     *   expected type.
     */
    fun validate(): Annotation = apply {
        if (validated) {
            return@apply
        }

        accept(
            object : Visitor<Unit> {
                override fun visitFileCitation(fileCitation: FileCitationAnnotation) {
                    fileCitation.validate()
                }

                override fun visitFilePath(filePath: FilePathAnnotation) {
                    filePath.validate()
                }
            }
        )
        validated = true
    }

    fun isValid(): Boolean =
        try {
            validate()
            true
        } catch (e: OpenAIInvalidDataException) {
            false
        }

    /**
     * Returns a score indicating how many valid values are contained in this object recursively.
     *
     * Used for best match union deserialization.
     */
    @JvmSynthetic
    internal fun validity(): Int =
        accept(
            object : Visitor<Int> {
                override fun visitFileCitation(fileCitation: FileCitationAnnotation) =
                    fileCitation.validity()

                override fun visitFilePath(filePath: FilePathAnnotation) = filePath.validity()

                override fun unknown(json: JsonValue?) = 0
            }
        )

    override fun equals(other: Any?): Boolean {
        if (this === other) {
            return true
        }

        return other is Annotation &&
            fileCitation == other.fileCitation &&
            filePath == other.filePath
    }

    override fun hashCode(): Int = Objects.hash(fileCitation, filePath)

    override fun toString(): String =
        when {
            fileCitation != null -> "Annotation{fileCitation=$fileCitation}"
            filePath != null -> "Annotation{filePath=$filePath}"
            _json != null -> "Annotation{_unknown=$_json}"
            else -> throw IllegalStateException("Invalid Annotation")
        }

    companion object {

        /**
         * A citation within the message that points to a specific quote from a specific File
         * associated with the assistant or the message. Generated when the assistant uses the
         * "file_search" tool to search files.
         */
        @JvmStatic
        fun ofFileCitation(fileCitation: FileCitationAnnotation) =
            Annotation(fileCitation = fileCitation)

        /**
         * A URL for the file that's generated when the assistant used the `code_interpreter` tool
         * to generate a file.
         */
        @JvmStatic fun ofFilePath(filePath: FilePathAnnotation) = Annotation(filePath = filePath)
    }

    /** An interface that defines how to map each variant of [Annotation] to a value of type [T]. */
    interface Visitor<out T> {

        /**
         * A citation within the message that points to a specific quote from a specific File
         * associated with the assistant or the message. Generated when the assistant uses the
         * "file_search" tool to search files.
         */
        fun visitFileCitation(fileCitation: FileCitationAnnotation): T

        /**
         * A URL for the file that's generated when the assistant used the `code_interpreter` tool
         * to generate a file.
         */
        fun visitFilePath(filePath: FilePathAnnotation): T

        /**
         * Maps an unknown variant of [Annotation] to a value of type [T].
         *
         * An instance of [Annotation] can contain an unknown variant if it was deserialized from
         * data that doesn't match any known variant. For example, if the SDK is on an older version
         * than the API, then the API may respond with new variants that the SDK is unaware of.
         *
         * @throws OpenAIInvalidDataException in the default implementation.
         */
        fun unknown(json: JsonValue?): T {
            throw OpenAIInvalidDataException("Unknown Annotation: $json")
        }
    }

    internal class Deserializer : BaseDeserializer<Annotation>(Annotation::class) {

        override fun ObjectCodec.deserialize(node: JsonNode): Annotation {
            val json = JsonValue.fromJsonNode(node)
            val type = json.asObject().getOrNull()?.get("type")?.asString()?.getOrNull()

            when (type) {
                "file_citation" -> {
                    return tryDeserialize(node, jacksonTypeRef<FileCitationAnnotation>())?.let {
                        Annotation(fileCitation = it, _json = json)
                    } ?: Annotation(_json = json)
                }
                "file_path" -> {
                    return tryDeserialize(node, jacksonTypeRef<FilePathAnnotation>())?.let {
                        Annotation(filePath = it, _json = json)
                    } ?: Annotation(_json = json)
                }
            }

            return Annotation(_json = json)
        }
    }

    internal class Serializer : BaseSerializer<Annotation>(Annotation::class) {

        override fun serialize(
            value: Annotation,
            generator: JsonGenerator,
            provider: SerializerProvider,
        ) {
            when {
                value.fileCitation != null -> generator.writeObject(value.fileCitation)
                value.filePath != null -> generator.writeObject(value.filePath)
                value._json != null -> generator.writeObject(value._json)
                else -> throw IllegalStateException("Invalid Annotation")
            }
        }
    }
}
