package de.tsschulz.tt_tagebuch.shared.api

import de.tsschulz.tt_tagebuch.shared.api.http.AuthedHttpClient
import de.tsschulz.tt_tagebuch.shared.api.models.LeaguePlayerStatDto
import de.tsschulz.tt_tagebuch.shared.api.models.Member
import de.tsschulz.tt_tagebuch.shared.api.models.LeagueTableRowDto
import de.tsschulz.tt_tagebuch.shared.api.models.FriendlyMatchSaveBody
import de.tsschulz.tt_tagebuch.shared.api.models.FriendlyMatchInvitationCreateBody
import de.tsschulz.tt_tagebuch.shared.api.models.FriendlyMatchInvitationDto
import de.tsschulz.tt_tagebuch.shared.api.models.ScheduleMatchDto
import de.tsschulz.tt_tagebuch.shared.api.models.UpdateMatchPlayersBody
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.patch
import io.ktor.client.request.post
import io.ktor.client.request.put
import io.ktor.client.request.setBody

class MatchesApi(
    private val client: AuthedHttpClient,
) {
    suspend fun listMatchesForLeagues(clubId: Int, seasonId: Int? = null): List<ScheduleMatchDto> {
        return client.http.get("/api/matches/leagues/$clubId/matches") {
            seasonId?.let { parameter("seasonid", it) }
        }.body()
    }

    suspend fun listMatchesForLeague(clubId: Int, leagueId: Int, scope: String = "own"): List<ScheduleMatchDto> {
        return client.http.get("/api/matches/leagues/$clubId/matches/$leagueId") {
            parameter("scope", scope)
        }.body()
    }

    suspend fun leagueTable(clubId: Int, leagueId: Int): List<LeagueTableRowDto> {
        return client.http.get("/api/matches/leagues/$clubId/table/$leagueId").body()
    }

    suspend fun getLeaguePlayerStats(clubId: Int, leagueId: Int, seasonId: Int? = null): List<LeaguePlayerStatDto> {
        return client.http.get("/api/matches/leagues/$clubId/stats/$leagueId") {
            seasonId?.let { parameter("seasonid", it) }
        }.body()
    }

    suspend fun updateMatchPlayers(matchId: Int, body: UpdateMatchPlayersBody) {
        client.http.patch("/api/matches/$matchId/players") {
            setBody(body)
        }
    }

    suspend fun listFriendlyMatches(clubId: Int): List<ScheduleMatchDto> {
        return client.http.get("/api/friendly-matches/$clubId").body()
    }

    suspend fun listSharedFriendlyMatches(clubId: Int): List<ScheduleMatchDto> {
        return client.http.get("/api/friendly-matches/shared/$clubId").body()
    }

    suspend fun listFriendlyMembers(clubId: Int): List<Member> {
        return client.http.get("/api/friendly-matches/$clubId/members/list").body()
    }

    suspend fun listSharedFriendlyMembers(clubId: Int, matchId: Int, side: String): List<Member> {
        return client.http.get("/api/friendly-matches/shared/$clubId/$matchId/members/$side").body()
    }

    suspend fun createFriendlyMatch(clubId: Int, body: FriendlyMatchSaveBody): ScheduleMatchDto {
        return client.http.post("/api/friendly-matches/$clubId") {
            setBody(body)
        }.body()
    }

    suspend fun updateFriendlyMatch(clubId: Int, matchId: Int, body: FriendlyMatchSaveBody): ScheduleMatchDto {
        return client.http.put("/api/friendly-matches/$clubId/$matchId") {
            setBody(body)
        }.body()
    }

    suspend fun updateSharedFriendlyMatch(clubId: Int, matchId: Int, body: FriendlyMatchSaveBody): ScheduleMatchDto {
        return client.http.put("/api/friendly-matches/shared/$clubId/$matchId") {
            setBody(body)
        }.body()
    }

    suspend fun updateSharedFriendlyMatchPlayers(clubId: Int, matchId: Int, body: UpdateMatchPlayersBody) {
        client.http.patch("/api/friendly-matches/shared/$clubId/$matchId/players") {
            setBody(body)
        }
    }

    suspend fun updateFriendlyMatchPlayers(clubId: Int, matchId: Int, body: UpdateMatchPlayersBody) {
        client.http.patch("/api/friendly-matches/$clubId/$matchId/players") {
            setBody(body)
        }
    }

    suspend fun deleteFriendlyMatch(clubId: Int, matchId: Int) {
        client.http.delete("/api/friendly-matches/$clubId/$matchId")
    }

    suspend fun deleteSharedFriendlyMatch(clubId: Int, matchId: Int) {
        client.http.delete("/api/friendly-matches/shared/$clubId/$matchId")
    }

    suspend fun createFriendlyInvitation(clubId: Int, body: FriendlyMatchInvitationCreateBody): FriendlyMatchInvitationDto {
        return client.http.post("/api/friendly-match-invitations/$clubId") {
            setBody(body)
        }.body()
    }

    suspend fun listIncomingFriendlyInvitations(clubId: Int): List<FriendlyMatchInvitationDto> {
        return client.http.get("/api/friendly-match-invitations/$clubId/incoming").body()
    }

    suspend fun listOutgoingFriendlyInvitations(clubId: Int): List<FriendlyMatchInvitationDto> {
        return client.http.get("/api/friendly-match-invitations/$clubId/outgoing").body()
    }

    suspend fun acceptFriendlyInvitation(clubId: Int, invitationId: Int): ScheduleMatchDto {
        return client.http.post("/api/friendly-match-invitations/$clubId/$invitationId/accept").body()
    }

    suspend fun declineFriendlyInvitation(clubId: Int, invitationId: Int): FriendlyMatchInvitationDto {
        return client.http.post("/api/friendly-match-invitations/$clubId/$invitationId/decline").body()
    }
}
