package de.tsschulz.tt_tagebuch.shared.api

import de.tsschulz.tt_tagebuch.shared.api.http.AuthedHttpClient
import de.tsschulz.tt_tagebuch.shared.api.models.Club
import de.tsschulz.tt_tagebuch.shared.api.models.UpdateClubSettingsBody
import io.ktor.client.call.body
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 kotlinx.serialization.Serializable

@Serializable
private data class CreateClubBody(val name: String)

class ClubsApi(
    private val client: AuthedHttpClient,
) {
    suspend fun listClubs(): List<Club> {
        return client.http.get("/api/clubs").body()
    }

    suspend fun createClub(name: String): Club {
        return client.http.post("/api/clubs") {
            setBody(CreateClubBody(name.trim()))
        }.body()
    }

    suspend fun getClub(clubId: Int): Club {
        return client.http.get("/api/clubs/$clubId").body()
    }

    suspend fun updateClubSettings(clubId: Int, body: UpdateClubSettingsBody) {
        client.http.put("/api/clubs/$clubId/settings") {
            setBody(body)
        }
    }

    suspend fun requestAccess(clubId: Int) {
        client.http.get("/api/clubs/request/$clubId")
    }
}

