@file:OptIn(ExperimentalUuidApi::class, ExperimentalTime::class)

package dev.zxq5.chatapp.android.api.model

import kotlinx.serialization.ExperimentalSerializationApi
import kotlinx.serialization.SerialName
import kotlinx.serialization.json.JsonClassDiscriminator
import kotlin.time.ExperimentalTime
import kotlin.time.Instant
import kotlinx.serialization.Serializable
import kotlin.uuid.ExperimentalUuidApi
import kotlin.uuid.Uuid

@OptIn(ExperimentalSerializationApi::class)
@Serializable
@JsonClassDiscriminator("type")
sealed class ChatEvent {

    @Serializable
    @SerialName("SendMessage")
    data class SendMessage(
        val data: Message
    ) : ChatEvent()

    @Serializable
    @SerialName("EditMessage")
    data class EditMessage(
        val data: EditMessageContent
    ) : ChatEvent()

    @Serializable
    @SerialName("MessageAppendContent")
    data class MessageAppendContent(
        val data: AppendContent
    ) : ChatEvent()
}

// tuple variants like (i64, ChatMsg) and (i64, String)
// need wrapper classes since kotlinx can't deserialise
// bare JSON arrays into data classes directly
@Serializable
data class EditMessageContent(
    val id: Uuid,
    val message: Message
)

@Serializable
data class AppendContent (
    val id: Uuid,
    val content: String
)

@Serializable
data class Message (
    val id: Uuid,
    val user_id: Int,
    val display_name: String,
    val text: String,
    val timestamp: Instant
)