package fr.teamflash.fencerjudgeback.services

import fr.teamflash.fencerjudgeback.entities.MatchBean
import fr.teamflash.fencerjudgeback.repositories.MatchRepository
import fr.teamflash.fencerjudgeback.websocket.controllers.MatchWebSocketController
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.context.annotation.Lazy
import org.springframework.stereotype.Service

@Service
class MatchService(
    @Autowired private val matchRepository: MatchRepository,
    @Lazy private val matchWebSocketController: MatchWebSocketController? = null
) {
    // Obtenir tous les matchs (public)
    fun getAll() : List<MatchBean> {
        println("MatchService.getMatchs")
        return matchRepository.findAll()
    }

    // Obtenir un match par id (public)
    fun getById(id: Long?): MatchBean? {
        println("MatchService.getMatchById : $id")

        if (id == null) {
            println("MatchService.getMatchById : Match not found")
            return null
        }

        return matchRepository.findById(id).get()
    }

    // Obtenir un ou plusieurs match(s) par joueurs (id) (public)
    fun getByPlayers(player1ID: Long?, player2ID: Long?): List<MatchBean> {
        println("MatchService.getMatchByPlayers : $player1ID - $player2ID")
        return matchRepository.findAll().filter { it.player1ID == player1ID && it.player2ID == player2ID }
    }

    // Ajouter un match (admin)
    fun createMatch(newMatch: MatchBean): MatchBean {
        println("MatchService.createMatch : $newMatch")
        val savedMatch = matchRepository.save(newMatch)
        // Broadcast the new match via WebSocket
        matchWebSocketController?.broadcastMatchUpdate(savedMatch, fr.teamflash.fencerjudgeback.websocket.models.MatchUpdateMessage.UpdateType.MATCH_START)

        return savedMatch
    }

    // Modifier un match (admin)
    fun updateMatch(id: Long?, newMatch: MatchBean): Int {
        println("MatchService.updateMatch : $newMatch")

        if (getById(id) == null) {
            return -1
        }

        val updatedMatch = newMatch.copy(id = id)
        val savedMatch = matchRepository.save(updatedMatch)
        // Broadcast the updated match via WebSocket
        matchWebSocketController?.broadcastMatchUpdate(savedMatch, fr.teamflash.fencerjudgeback.websocket.models.MatchUpdateMessage.UpdateType.SCORE_UPDATE)
        return 1
    }

    // Supprimer un match (admin)
    fun deleteMatchById(id: Long?) : Int? {
        println("MatchService.deleteMatchById : $id")

        val match = getById(id)
        if (match == null) {
            println("MatchService.deleteMatchById : Match not found")
            return -1
        }

        // Broadcast the match deletion via WebSocket before deleting
        matchWebSocketController?.broadcastMatchUpdate(match, fr.teamflash.fencerjudgeback.websocket.models.MatchUpdateMessage.UpdateType.MATCH_CANCEL)

        matchRepository.deleteById(id!!)
        return 1
    }

    fun getMatchesByCity(city: String): List<MatchBean>? {
        println("MatchService.getMatchesByCity : $city")

        return matchRepository.findAll()
            .filter { it.city == city }
    }

    fun getMatchesByCountry(country: String): List<MatchBean>? {
        println("MatchService.getMatchesByCountry : $country")

        return matchRepository.findAll()
            .filter { it.country == country }
    }

    fun addMatch(match:MatchBean) {
        matchRepository.save(match)
    }
}
