package top.r3944realms.ltdmanager.whitelist.response

import io.ktor.http.HttpStatusCode
import kotlinx.serialization.Serializable
import kotlinx.serialization.Transient
import kotlinx.serialization.json.Json
import kotlinx.serialization.modules.SerializersModule
import kotlinx.serialization.modules.polymorphic
import top.r3944realms.ltdmanager.core.client.response.IResponse

@Serializable
abstract class WhitelistSystemResponse(
    @Transient
    override val httpStatusCode: HttpStatusCode = HttpStatusCode.OK,
    @Transient
    override val createTime: Long = System.currentTimeMillis()
) : IResponse {
    companion object {
        inline fun <reified T : WhitelistSystemResponse> decode(jsonString: String): T {
            return json.decodeFromString(jsonString)
        }

        val json: Json by lazy {
            Json {
                ignoreUnknownKeys = true
                serializersModule = SerializersModule {
                    polymorphic(WhitelistSystemResponse::class) {
                        subclass(FailedWhitelistSystemResponse.Default::class, FailedWhitelistSystemResponse.Default.serializer())
                        subclass(WhitelistApiResponse::class, WhitelistApiResponse.serializer())
                    }
                }
            }
        }
    }
}
