package de.kif.common.model

import de.kif.common.SearchElement
import kotlinx.serialization.Serializable

@Serializable
data class User(
    override val id: Long?,
    val username: String,
    val password: String,
    val permissions: Set<Permission>,
    override val createdAt: Long = 0,
    override val updateAt: Long = 0
) : Model {



    fun checkPermission(permission: Permission): Boolean {
        return permission in permissions || Permission.ADMIN in permissions
    }

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

    override fun equals(other: Any?): Boolean {
        if (this === other) return true
        if (other == null || this::class != other::class) return false

        other as User

        if (id != other.id) return false
        if (username != other.username) return false
        if (password != other.password) return false
        if (permissions != other.permissions) return false

        return true
    }

    fun equalsIgnoreId(other: User): Boolean = copy(id = null) == other.copy(id = null)

    override fun hashCode(): Int {
        var result = id?.hashCode() ?: 0
        result = 31 * result + username.hashCode()
        result = 31 * result + password.hashCode()
        result = 31 * result + permissions.hashCode()
        return result
    }
}
