package de.tsschulz.tt_tagebuch.shared.api

import de.tsschulz.tt_tagebuch.shared.api.http.ApiException
import de.tsschulz.tt_tagebuch.shared.api.http.AuthedHttpClient
import de.tsschulz.tt_tagebuch.shared.api.models.MemberTransferConfigEnvelope
import de.tsschulz.tt_tagebuch.shared.api.models.MemberTransferConfigSaveBody
import io.ktor.client.call.body
import io.ktor.client.request.delete
import io.ktor.client.request.get
import io.ktor.client.request.post
import io.ktor.client.request.setBody

class MemberTransferConfigApi(
    private val client: AuthedHttpClient,
) {
    suspend fun get(clubId: Int): MemberTransferConfigEnvelope? {
        return try {
            client.http.get("/api/member-transfer-config/$clubId").body()
        } catch (e: ApiException) {
            if (e.statusCode == 404) null else throw e
        }
    }

    suspend fun save(clubId: Int, body: MemberTransferConfigSaveBody): MemberTransferConfigEnvelope {
        return client.http.post("/api/member-transfer-config/$clubId") {
            setBody(body)
        }.body()
    }

    suspend fun delete(clubId: Int): MemberTransferConfigEnvelope {
        return client.http.delete("/api/member-transfer-config/$clubId").body()
    }
}
