package de.tsschulz.tt_tagebuch.shared.api

import de.tsschulz.tt_tagebuch.shared.api.http.AuthedHttpClient
import de.tsschulz.tt_tagebuch.shared.api.models.CreateTrainingTimeBody
import de.tsschulz.tt_tagebuch.shared.api.models.TrainingGroupDto
import de.tsschulz.tt_tagebuch.shared.api.models.TrainingTimeDto
import de.tsschulz.tt_tagebuch.shared.api.models.UpdateTrainingTimeBody
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.put
import io.ktor.client.request.setBody
import io.ktor.http.ContentType
import io.ktor.http.contentType

class TrainingTimesApi(
    private val client: AuthedHttpClient,
) {
    suspend fun listGroupsWithTimes(clubId: Int): List<TrainingGroupDto> {
        return client.http.get("/api/training-times/$clubId").body()
    }

    suspend fun createTime(clubId: Int, body: CreateTrainingTimeBody): TrainingTimeDto {
        return client.http.post("/api/training-times/$clubId") {
            contentType(ContentType.Application.Json)
            setBody(body)
        }.body()
    }

    suspend fun updateTime(clubId: Int, timeId: Int, body: UpdateTrainingTimeBody): TrainingTimeDto {
        return client.http.put("/api/training-times/$clubId/$timeId") {
            contentType(ContentType.Application.Json)
            setBody(body)
        }.body()
    }

    suspend fun deleteTime(clubId: Int, timeId: Int) {
        client.http.delete("/api/training-times/$clubId/$timeId")
    }
}
