package top.r3944realms.ltdmanager.whitelist.request

import io.ktor.http.HttpMethod
import io.ktor.http.HttpStatusCode
import kotlinx.serialization.Serializable
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

@Serializable
class WhitelistListRequest : WhitelistSystemRequest() {

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

    override fun path(): String = "/api/whitelist/list"

    override fun method(): HttpMethod = HttpMethod.Get

    override fun queryParameters(): Map<String, String> = mapOf("all" to "true")

    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 = "解析列表响应失败: ${e.message}")
            )
        }
    }

    override fun expectedResponseType(): String = "whitelist_list"
    override fun expectedFailureType(): String = "default_failure"
}
