package de.tsschulz.tt_tagebuch.shared.api

import de.tsschulz.tt_tagebuch.shared.api.http.AuthedHttpClient
import de.tsschulz.tt_tagebuch.shared.api.models.MyTtConfigureLeagueBody
import de.tsschulz.tt_tagebuch.shared.api.models.MyTtConfigureTeamBody
import de.tsschulz.tt_tagebuch.shared.api.models.MyTtFetchJobEnvelopeDto
import de.tsschulz.tt_tagebuch.shared.api.models.MyTtFetchJobStartDto
import de.tsschulz.tt_tagebuch.shared.api.models.MyTtFetchTeamDataBody
import de.tsschulz.tt_tagebuch.shared.api.models.MyTtParseUrlBody
import de.tsschulz.tt_tagebuch.shared.api.models.MyTischtennisAccountEnvelope
import de.tsschulz.tt_tagebuch.shared.api.models.MyTischtennisAccountSaveResponse
import de.tsschulz.tt_tagebuch.shared.api.models.MyTischtennisAccountUpsertBody
import de.tsschulz.tt_tagebuch.shared.api.models.MyTischtennisStatusDto
import de.tsschulz.tt_tagebuch.shared.api.models.MyTischtennisVerifyBody
import de.tsschulz.tt_tagebuch.shared.api.models.MyTischtennisVerifyResponseDto
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.setBody
import io.ktor.http.ContentType
import io.ktor.http.contentType
import kotlinx.serialization.json.JsonObject

class MyTischtennisApi(
    private val client: AuthedHttpClient,
) {
    suspend fun getAccount(): MyTischtennisAccountEnvelope =
        client.http.get("/api/mytischtennis/account").body()

    suspend fun getStatus(): MyTischtennisStatusDto =
        client.http.get("/api/mytischtennis/status").body()

    suspend fun upsertAccount(body: MyTischtennisAccountUpsertBody): MyTischtennisAccountSaveResponse =
        client.http.post("/api/mytischtennis/account") {
            contentType(ContentType.Application.Json)
            setBody(body)
        }.body()

    suspend fun verifyLogin(password: String? = null): MyTischtennisVerifyResponseDto =
        client.http.post("/api/mytischtennis/verify") {
            contentType(ContentType.Application.Json)
            setBody(
                if (password != null) {
                    MyTischtennisVerifyBody(password = password)
                } else {
                    JsonObject(emptyMap())
                },
            )
        }.body()

    suspend fun deleteAccount() {
        client.http.delete("/api/mytischtennis/account")
    }

    suspend fun parseUrl(url: String): JsonObject =
        client.http.post("/api/mytischtennis/parse-url") {
            contentType(ContentType.Application.Json)
            setBody(MyTtParseUrlBody(url))
        }.body()

    suspend fun configureTeam(body: MyTtConfigureTeamBody): JsonObject =
        client.http.post("/api/mytischtennis/configure-team") {
            contentType(ContentType.Application.Json)
            setBody(body)
        }.body()

    suspend fun configureLeague(body: MyTtConfigureLeagueBody): JsonObject =
        client.http.post("/api/mytischtennis/configure-league") {
            contentType(ContentType.Application.Json)
            setBody(body)
        }.body()

    suspend fun startFetchTeamDataJob(clubTeamId: Int): MyTtFetchJobStartDto =
        client.http.post("/api/mytischtennis/fetch-team-data/async") {
            contentType(ContentType.Application.Json)
            setBody(MyTtFetchTeamDataBody(clubTeamId))
        }.body()

    suspend fun getFetchTeamDataJob(jobId: String): MyTtFetchJobEnvelopeDto =
        client.http.get("/api/mytischtennis/fetch-team-data/jobs/$jobId").body()
}
