package de.tsschulz.tt_tagebuch.shared.api

import de.tsschulz.tt_tagebuch.shared.api.http.AuthedHttpClient
import de.tsschulz.tt_tagebuch.shared.api.models.MemberOrderCreateBody
import de.tsschulz.tt_tagebuch.shared.api.models.MemberOrderEnvelope
import de.tsschulz.tt_tagebuch.shared.api.models.MemberOrderPatchBody
import de.tsschulz.tt_tagebuch.shared.api.models.MemberOrdersListEnvelope
import io.ktor.client.call.body
import io.ktor.client.request.get
import io.ktor.client.request.patch
import io.ktor.client.request.post
import io.ktor.client.request.setBody
import io.ktor.http.ContentType
import io.ktor.http.contentType

class MemberOrdersApi(
    private val client: AuthedHttpClient,
) {
    suspend fun listGlobal(): MemberOrdersListEnvelope =
        client.http.get("/api/member-orders/global").body()

    suspend fun listForMember(clubId: Int, memberId: Int): MemberOrdersListEnvelope =
        client.http.get("/api/member-orders/$clubId/$memberId").body()

    suspend fun create(clubId: Int, memberId: Int, body: MemberOrderCreateBody): MemberOrderEnvelope =
        client.http.post("/api/member-orders/$clubId/$memberId") {
            contentType(ContentType.Application.Json)
            setBody(body)
        }.body()

    suspend fun update(clubId: Int, memberId: Int, orderId: Int, body: MemberOrderPatchBody): MemberOrderEnvelope =
        client.http.patch("/api/member-orders/$clubId/$memberId/$orderId") {
            contentType(ContentType.Application.Json)
            setBody(body)
        }.body()
}
