package de.kif.common.model

import de.kif.common.SearchElement
import kotlinx.serialization.Serializable
import kotlin.random.Random

@Serializable
data class Post(
    override val id: Long? = null,
    val name: String,
    val content: String,
    val url: String,
    val image: String?,
    val pinned: Boolean,
    val hideOnProjector: Boolean,
    override val createdAt: Long = 0,
    override val updateAt: Long = 0
) : Model {

    override fun createSearch() = SearchElement(
        mapOf(
            "name" to name
        )
    )

    companion object {
        private const val chars = "abcdefghijklmnopqrstuvwxyz"
        private const val length = 32

        fun generateUrl() = (0 until length).asSequence()
            .map { Random.nextInt(chars.length) }
            .map { chars[it] }
            .map {
                if (Random.nextBoolean()) it else it.toUpperCase()
            }.joinToString("")
    }
}
