package de.tsschulz.tt_tagebuch.shared.api

import de.tsschulz.tt_tagebuch.shared.api.http.AuthedHttpClient
import de.tsschulz.tt_tagebuch.shared.api.models.PredefinedActivityDto
import de.tsschulz.tt_tagebuch.shared.api.models.PredefinedActivityUpsertBody
import io.ktor.client.call.body
import io.ktor.client.request.get
import io.ktor.client.request.parameter
import io.ktor.client.request.post
import io.ktor.client.request.put
import io.ktor.client.request.setBody

class PredefinedActivitiesApi(
    private val client: AuthedHttpClient,
) {
    suspend fun list(scope: String? = null): List<PredefinedActivityDto> {
        return client.http.get("/api/predefined-activities") {
            if (scope != null) parameter("scope", scope)
        }.body()
    }

    suspend fun search(query: String, limit: Int = 20): List<PredefinedActivityDto> {
        return client.http.get("/api/predefined-activities/search/query") {
            parameter("q", query)
            parameter("limit", limit)
        }.body()
    }

    suspend fun getById(id: Int): PredefinedActivityDto {
        return client.http.get("/api/predefined-activities/$id").body()
    }

    suspend fun create(body: PredefinedActivityUpsertBody): PredefinedActivityDto {
        return client.http.post("/api/predefined-activities") {
            setBody(body)
        }.body()
    }

    suspend fun update(id: Int, body: PredefinedActivityUpsertBody): PredefinedActivityDto {
        return client.http.put("/api/predefined-activities/$id") {
            setBody(body)
        }.body()
    }
}
