package de.tsschulz.tt_tagebuch.shared.api

import de.tsschulz.tt_tagebuch.shared.api.http.AuthedHttpClient
import de.tsschulz.tt_tagebuch.shared.api.models.ClubLeagueOptionDto
import de.tsschulz.tt_tagebuch.shared.api.models.ClubTeamCreateBody
import de.tsschulz.tt_tagebuch.shared.api.models.ClubTeamDto
import de.tsschulz.tt_tagebuch.shared.api.models.ClubTeamLineupRowDto
import de.tsschulz.tt_tagebuch.shared.api.models.ClubTeamUpdateBody
import de.tsschulz.tt_tagebuch.shared.api.models.TeamLineupUpdateBody
import io.ktor.client.call.body
import io.ktor.client.request.delete
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 ClubTeamsApi(
    private val client: AuthedHttpClient,
) {
    suspend fun listClubTeams(clubId: Int, seasonId: Int? = null): List<ClubTeamDto> {
        return client.http.get("/api/club-teams/club/$clubId") {
            seasonId?.let { parameter("seasonid", it) }
        }.body()
    }

    suspend fun listLeagues(clubId: Int, seasonId: Int? = null): List<ClubLeagueOptionDto> {
        return client.http.get("/api/club-teams/leagues/$clubId") {
            seasonId?.let { parameter("seasonid", it) }
        }.body()
    }

    suspend fun getClubTeam(clubTeamId: Int): ClubTeamDto {
        return client.http.get("/api/club-teams/$clubTeamId").body()
    }

    suspend fun createClubTeam(clubId: Int, body: ClubTeamCreateBody): ClubTeamDto {
        return client.http.post("/api/club-teams/club/$clubId") {
            setBody(body)
        }.body()
    }

    suspend fun updateClubTeam(clubTeamId: Int, body: ClubTeamUpdateBody): ClubTeamDto {
        return client.http.put("/api/club-teams/$clubTeamId") {
            setBody(body)
        }.body()
    }

    suspend fun deleteClubTeam(clubTeamId: Int) {
        client.http.delete("/api/club-teams/$clubTeamId")
    }

    suspend fun getLineup(clubTeamId: Int, half: String): List<ClubTeamLineupRowDto> {
        return client.http.get("/api/club-teams/$clubTeamId/lineup") {
            parameter("half", half)
        }.body()
    }

    suspend fun updateLineup(clubTeamId: Int, body: TeamLineupUpdateBody): List<ClubTeamLineupRowDto> {
        return client.http.put("/api/club-teams/$clubTeamId/lineup") {
            setBody(body)
        }.body()
    }
}
