package dp.kotul.solrapiapplication.helper

import dp.kotul.solrapiapplication.repository.LanguageRepository
import dp.kotul.starter.service.AuthorizationService
import org.springframework.http.HttpStatus.NOT_FOUND
import org.springframework.stereotype.Component
import org.springframework.web.server.ResponseStatusException

interface CollectionHelper {
    fun getCollection(laeId: Int): String
}

@Component
class CollectionHelperImpl(
    val authService: AuthorizationService,
    val languageRepository: LanguageRepository
) : CollectionHelper {

    override fun getCollection(laeId: Int): String {
        val user = authService.getUser()

        val language = languageRepository.getLanguages().firstOrNull { it.id == laeId }
            ?: throw ResponseStatusException(NOT_FOUND, "Language $laeId not found")

        return "com_id_${user.comId}_${language.shortName.toLowerCase()}"
    }
}