package de.tsschulz.tt_tagebuch.shared.api.models

import de.tsschulz.tt_tagebuch.shared.api.serialization.LenientIntListSerializer
import kotlinx.serialization.Serializable

@Serializable
data class ClubTeamLeagueDto(
    val id: Int = 0,
    val name: String = "",
    val myTischtennisGroupId: String? = null,
    val association: String? = null,
    val groupname: String? = null,
)

@Serializable
data class ClubTeamSeasonDto(
    val id: Int? = null,
    val season: String = "",
)

/** Option aus `GET /api/club-teams/leagues/:clubId`. */
@Serializable
data class ClubLeagueOptionDto(
    val id: Int,
    val name: String = "",
    val seasonId: Int? = null,
)

@Serializable
data class ClubTeamCreateBody(
    val name: String,
    val leagueId: Int? = null,
    val seasonId: Int? = null,
    val teamGender: String? = null,
    val teamAgeGroup: String? = null,
    val plannedLeagueName: String? = null,
)

@Serializable
data class ClubTeamUpdateBody(
    val name: String? = null,
    val leagueId: Int? = null,
    val seasonId: Int? = null,
    val teamGender: String? = null,
    val teamAgeGroup: String? = null,
    val plannedLeagueName: String? = null,
)

@Serializable
data class ClubTeamDto(
    val id: Int,
    val name: String = "",
    val clubId: Int = 0,
    val leagueId: Int? = null,
    val seasonId: Int? = null,
    val myTischtennisTeamId: String? = null,
    val teamGender: String? = null,
    val teamAgeGroup: String? = null,
    val plannedLeagueName: String? = null,
    val league: ClubTeamLeagueDto? = null,
    val season: ClubTeamSeasonDto? = null,
)

@Serializable
data class ScheduleTeamNameDto(
    val name: String = "",
)

@Serializable
data class ScheduleLocationDto(
    val name: String = "",
    val address: String = "",
    val city: String = "",
    val zip: String = "",
)

@Serializable
data class ScheduleLeagueDetailsDto(
    val name: String = "",
)

@Serializable
data class ScheduleMatchDto(
    val id: Int,
    val friendlyMatchId: Int? = null,
    val sharedMatchId: Int? = null,
    val isFriendly: Boolean = false,
    val isSharedFriendly: Boolean = false,
    val date: String? = null,
    val time: String? = null,
    val homeClubId: Int? = null,
    val guestClubId: Int? = null,
    val homeTeamId: Int? = null,
    val guestTeamId: Int? = null,
    val locationId: Int? = null,
    val leagueId: Int? = null,
    val code: String? = null,
    val homePin: String? = null,
    val guestPin: String? = null,
    val homeMatchPoints: Int = 0,
    val guestMatchPoints: Int = 0,
    val isCompleted: Boolean = false,
    val isLocked: Boolean = false,
    val pdfUrl: String? = null,
    val matchSystem: String? = null,
    val singlesCount: Int = 12,
    val doublesCount: Int = 4,
    val winningSets: Int = 3,
    val homeParticipants: List<FriendlyParticipantDto> = emptyList(),
    val guestParticipants: List<FriendlyParticipantDto> = emptyList(),
    val resultDetails: List<FriendlyResultRowDto> = emptyList(),
    @Serializable(with = LenientIntListSerializer::class)
    val playersReady: List<Int> = emptyList(),
    @Serializable(with = LenientIntListSerializer::class)
    val playersPlanned: List<Int> = emptyList(),
    @Serializable(with = LenientIntListSerializer::class)
    val playersPlayed: List<Int> = emptyList(),
    val homeTeam: ScheduleTeamNameDto? = null,
    val guestTeam: ScheduleTeamNameDto? = null,
    val location: ScheduleLocationDto? = null,
    val leagueDetails: ScheduleLeagueDetailsDto? = null,
)

@Serializable
data class FriendlyParticipantDto(
    val type: String = "manual",
    val memberId: Int? = null,
    val firstName: String = "",
    val lastName: String = "",
)

@Serializable
data class FriendlyResultRowDto(
    val id: String = "",
    val type: String = "single",
    val homeName: String = "",
    val guestName: String = "",
    val sets: List<String> = emptyList(),
    val completed: Boolean = false,
)

@Serializable
data class FriendlyMatchSaveBody(
    val date: String,
    val time: String? = null,
    val homeTeamName: String,
    val guestTeamName: String,
    val matchSystem: String = "Braunschweiger System",
    val winningSets: Int = 3,
    val homeParticipants: List<FriendlyParticipantDto> = emptyList(),
    val guestParticipants: List<FriendlyParticipantDto> = emptyList(),
    val homeMatchPoints: Int = 0,
    val guestMatchPoints: Int = 0,
    val isCompleted: Boolean = false,
    val resultDetails: List<FriendlyResultRowDto> = emptyList(),
)

@Serializable
data class FriendlyMatchInvitationDto(
    val id: Int,
    val fromClubId: Int,
    val toClubId: Int,
    val proposedDate: String,
    val proposedStartTime: String? = null,
    val proposedMatchName: String,
    val message: String? = null,
    val status: String = "pending",
    val createdByUserId: Int? = null,
    val acceptedByUserId: Int? = null,
    val acceptedAt: String? = null,
)

@Serializable
data class FriendlyMatchInvitationCreateBody(
    val toClubId: Int,
    val date: String,
    val startTime: String? = null,
    val matchName: String,
    val message: String? = null,
)

@Serializable
data class LeagueTableRowDto(
    val teamId: Int,
    val teamName: String = "",
    val setsWon: Int = 0,
    val setsLost: Int = 0,
    /** z. B. \"3:1\" */
    val matchPoints: String = "",
    val tablePoints: String = "",
    val pointRatio: String = "",
)

@Serializable
data class UpdateMatchPlayersBody(
    val clubId: Int,
    val playersReady: List<Int> = emptyList(),
    val playersPlanned: List<Int> = emptyList(),
    val playersPlayed: List<Int> = emptyList(),
)

enum class ScheduleMatchScope {
    Own,
    All,
    Other,
}

enum class ScheduleViewMode {
    Team,
    Overall,
    Adult,
    Friendly,
}
