package top.r3944realms.ltdmanager.whitelist.request

import io.ktor.http.HttpMethod
import io.ktor.http.HttpStatusCode
import kotlinx.serialization.Serializable
import kotlinx.serialization.Transient
import top.r3944realms.ltdmanager.core.client.response.ResponseResult
import top.r3944realms.ltdmanager.whitelist.response.FailedWhitelistSystemResponse
import top.r3944realms.ltdmanager.whitelist.response.WhitelistApiResponse
import top.r3944realms.ltdmanager.whitelist.response.WhitelistSystemResponse

enum class WhitelistAction(val operation: String) {
    APPROVE("approve"),
    REJECT("reject"),
    REMOVE("remove"),
}

@Serializable
class WhitelistActionRequest(
    @Transient
    private val playerId: Int = 0,
    @Transient
    private val action: WhitelistAction = WhitelistAction.APPROVE,
) : WhitelistSystemRequest() {

    override fun toJSON(): String = "{}"

    override fun path(): String = "/api/whitelist/${action.operation}/$playerId"

    override fun method(): HttpMethod = when (action) {
        WhitelistAction.REMOVE -> HttpMethod.Delete
        else -> HttpMethod.Post
    }

    override fun getResponse(
        responseJson: String,
        httpStatusCode: HttpStatusCode
    ): ResponseResult<WhitelistApiResponse, FailedWhitelistSystemResponse.Default> {
        return try {
            val response = WhitelistSystemResponse.decode(responseJson) as? WhitelistApiResponse
                ?: throw IllegalArgumentException("响应类型不匹配")
            ResponseResult.Success(response)
        } catch (e: Exception) {
            ResponseResult.Failure(
                FailedWhitelistSystemResponse.Default(failedMessage = "解析${action.operation}响应失败: ${e.message}")
            )
        }
    }

    override fun expectedResponseType(): String = "whitelist_${action.operation}"
    override fun expectedFailureType(): String = "default_failure"
    override fun shouldRetryOnFailure(): Boolean = false
}
