// this file is @generated
package com.svix.kotlin.internal

import com.svix.kotlin.StringAnyMapSerializer
import com.svix.kotlin.SvixHttpClient
import com.svix.kotlin.models.ApplicationIn
import com.svix.kotlin.models.MessageIn
import kotlinx.serialization.Serializable
import kotlinx.serialization.json.JsonElement
import kotlinx.serialization.json.JsonObject
import kotlinx.serialization.json.JsonPrimitive

class Message(private val client: SvixHttpClient) {
    val pollerv2: MessagePollerv2 = MessagePollerv2(client)
}

/**
 * Creates a [MessageIn] with a pre-serialized payload.
 *
 * The payload is not normalized on the server. Normally, payloads are required to be JSON, and Svix
 * will minify the payload before sending the webhooks (for example, by removing extraneous
 * whitespace or unnecessarily escaped characters in strings). With this function, the payload will
 * be sent "as is", without any minification or other processing.
 *
 * @param payload Serialized message payload
 * @param contentType The value to use for the Content-Type header of the webhook sent by Svix,
 *   overwriting the default of `application/json` if specified
 *
 * See the class documentation for details about the other parameters.
 */
fun messageInRaw(
    eventType: String,
    payload: String,
    contentType: String? = null,
    application: ApplicationIn? = null,
    channels: Set<String>? = null,
    eventId: String? = null,
    payloadRetentionHours: Long? = null,
    payloadRetentionPeriod: Long? = 90L,
    tags: Set<String>? = null,
    transformationsParams: Map<String, JsonElement> = mapOf(),
): MessageIn {
    val transformationsParams = transformationsParams.toMutableMap()
    transformationsParams["rawPayload"] = JsonPrimitive(payload)
    if (contentType != null) {
        val headers = mapOf("content-type" to JsonPrimitive(contentType))
        transformationsParams["headers"] = JsonObject(headers)
    }

    return MessageIn(
        eventType = eventType,
        payload = "",
        application = application,
        channels = channels,
        eventId = eventId,
        payloadRetentionHours = payloadRetentionHours,
        payloadRetentionPeriod = payloadRetentionPeriod,
        tags = tags,
        transformationsParams = JsonObject(transformationsParams),
    )
}

@Serializable
private data class MessageInInternal(
    val application: ApplicationIn? = null,
    val channels: Set<String>? = null,
    val eventId: String? = null,
    val eventType: String,
    @Serializable(with = StringAnyMapSerializer::class) var payload: Map<String, Any>,
    val payloadRetentionHours: Long? = null,
    val payloadRetentionPeriod: Long? = null,
    val tags: Set<String>? = null,
    @Serializable(with = StringAnyMapSerializer::class)
    var transformationsParams: Map<String, Any>? = null,
)
