package dev.inmo.postssystem.features.roles.server

import dev.inmo.postssystem.features.roles.common.*
import dev.inmo.postssystem.features.users.common.User
import io.ktor.application.ApplicationCall

interface RolesChecker<T : Role> {
    val key: String

    suspend operator fun ApplicationCall.invoke(
        usersRolesStorage: ReadRolesStorage<T>,
        user: User
    ): Boolean

    companion object {
        fun default(
            key: String,
            role: Role
        ): RolesChecker<Role> = object : RolesChecker<Role> {
            override val key: String
                get() = key

            override suspend fun ApplicationCall.invoke(
                usersRolesStorage: ReadRolesStorage<Role>,
                user: User
            ): Boolean = usersRolesStorage.contains(user.id, role)
        }
    }
}
