package dev.inmo.postssystem.features.roles.client

import dev.inmo.postssystem.features.roles.common.*
import dev.inmo.micro_utils.ktor.client.UnifiedRequester
import dev.inmo.micro_utils.ktor.common.buildStandardUrl
import kotlinx.serialization.KSerializer
import kotlinx.serialization.builtins.serializer

class WriteClientRolesStorage<T : Role>(
    private val baseUrl: String,
    private val unifiedRequester: UnifiedRequester,
    private val serializer: KSerializer<T>
) : WriteRolesStorage<T> {
    private val wrapperSerializer = RolesStorageIncludeExcludeWrapper.serializer(
        serializer
    )
    private val userRolesFullUrl = buildStandardUrl(
        baseUrl,
        usersRolesRootPathPart
    )
    private val includeFullUrl = buildStandardUrl(
        userRolesFullUrl,
        usersRolesIncludePathPart
    )
    private val excludeFullUrl = buildStandardUrl(
        userRolesFullUrl,
        usersRolesExcludePathPart
    )

    override suspend fun include(
        subject: RoleSubject,
        role: T
    ): Boolean = unifiedRequester.unipost(
        includeFullUrl,
        wrapperSerializer to RolesStorageIncludeExcludeWrapper(
            subject, role
        ),
        Boolean.serializer()
    )

    override suspend fun exclude(
        subject: RoleSubject,
        role: T
    ): Boolean  = unifiedRequester.unipost(
        excludeFullUrl,
        wrapperSerializer to RolesStorageIncludeExcludeWrapper(
            subject, role
        ),
        Boolean.serializer()
    )
}
