package de.tsschulz.tt_tagebuch.shared.api

import de.tsschulz.tt_tagebuch.shared.api.http.AuthedHttpClient
import de.tsschulz.tt_tagebuch.shared.api.models.DiaryTrainingParticipant
import de.tsschulz.tt_tagebuch.shared.api.models.ParticipantGroupRequest
import de.tsschulz.tt_tagebuch.shared.api.models.ParticipantMutationRequest
import io.ktor.client.call.body
import de.tsschulz.tt_tagebuch.shared.api.models.ParticipantStatusRequest
import io.ktor.client.request.get
import io.ktor.client.request.post
import io.ktor.client.request.put
import io.ktor.client.request.setBody

class ParticipantsApi(
    private val client: AuthedHttpClient,
) {
    suspend fun listForDate(diaryDateId: Int): List<DiaryTrainingParticipant> {
        return client.http.get("/api/participants/$diaryDateId").body()
    }

    suspend fun add(diaryDateId: Int, memberId: Int): DiaryTrainingParticipant {
        return client.http.post("/api/participants/add") {
            setBody(ParticipantMutationRequest(diaryDateId, memberId))
        }.body()
    }

    suspend fun remove(diaryDateId: Int, memberId: Int) {
        client.http.post("/api/participants/remove") {
            setBody(ParticipantMutationRequest(diaryDateId, memberId))
        }
    }

    suspend fun updateAttendanceStatus(diaryDateId: Int, memberId: Int, attendanceStatus: String) {
        client.http.put("/api/participants/$diaryDateId/$memberId/status") {
            setBody(ParticipantStatusRequest(attendanceStatus = attendanceStatus))
        }
    }

    suspend fun updateParticipantGroup(diaryDateId: Int, memberId: Int, groupId: Int?) {
        client.http.put("/api/participants/$diaryDateId/$memberId/group") {
            setBody(ParticipantGroupRequest(groupId = groupId))
        }
    }
}
