package dp.kotul.templategroupsbackend.facade

import dp.kotul.solrapimodel.solrsearch.AssetType
import dp.kotul.solrapimodel.solrsearch.SolrUtils
import dp.kotul.starter.filter.RequestCorrelation
import dp.kotul.starter.messaging.model.CREATIVE_GENERATE_TOPIC
import dp.kotul.starter.messaging.model.TEMPLATE_GROUPS_EXCHANGE
import dp.kotul.starter.messaging.model.UserInBaseMessage
import dp.kotul.starter.messaging.model.templategroups.creative.generate.GenerateCreativeMessage
import dp.kotul.starter.messaging.model.templategroups.creative.generate.TemplateGroupInGenerateCreativeMessage
import dp.kotul.starter.messaging.model.templategroups.creative.generate.TemplateInGenerateCreativeMessage
import dp.kotul.starter.messaging.model.templategroups.creative.generate.TemplateVariableInGenerateCreativeMessage
import dp.kotul.starter.messaging.service.Rabbit
import dp.kotul.starter.service.AuthorizationService
import dp.kotul.templategroupsbackend.helper.SolrSearchHelper
import dp.kotul.templategroupsbackend.model.request.TemplateGroupGenerateCreativeRequest
import dp.kotul.templategroupsbackend.repository.TemplateGroupRepository
import org.springframework.data.repository.findByIdOrNull
import org.springframework.stereotype.Component
import org.springframework.transaction.annotation.Transactional

interface CreativeFacade {
    fun generateCreatives(templateGroupGenerateCreativeRequest: TemplateGroupGenerateCreativeRequest, laeId: Int?)
}

@Component
@Transactional
class CreativeFacadeImpl(
        val authService: AuthorizationService,
        val templateGroupRepository: TemplateGroupRepository,
        val solrSearchHelper: SolrSearchHelper,
        val rabbit: Rabbit
) : CreativeFacade {

    override fun generateCreatives(templateGroupGenerateCreativeRequest: TemplateGroupGenerateCreativeRequest, laeId: Int?) {
        val user = authService.getUser()
        val languageId = laeId ?: user.laeId

        val templateGroup = templateGroupRepository.findByIdOrNull(templateGroupGenerateCreativeRequest.templateGroupId) ?: return
        val templateVariables = templateGroup.templates.flatMap { template -> template.templateVariables.map { it } }

        val solrTemplatesResponse = solrSearchHelper.getSolrTemplatesResponse(languageId, templateGroup.templates)

        val solrTemplates = solrTemplatesResponse!!.templates

        rabbit.distributeMessage(TEMPLATE_GROUPS_EXCHANGE, CREATIVE_GENERATE_TOPIC, GenerateCreativeMessage(
                RequestCorrelation.getId(),
                UserInBaseMessage(user.comId, user.usrId, user.instanceId, user.sessionId),
                languageId,
                TemplateGroupInGenerateCreativeMessage(
                        templateGroupId = templateGroupGenerateCreativeRequest.templateGroupId,
                        templates = templateGroupGenerateCreativeRequest.templates.map { templateGenerateCreativeRequest ->
                            val inCurrentTemplateTemplateVariables = templateGenerateCreativeRequest.templateVariables.filter { it.includedInCurrentTemplate && it.value != null }

                            val solrTemplate = solrTemplates.first { it.id == SolrUtils.getUniqueSolrDocumentId(templateGenerateCreativeRequest.templateExtId,
                                AssetType.TEMPLATE, templateGenerateCreativeRequest.templateType.name, languageId) }

                            TemplateInGenerateCreativeMessage(
                                    type = templateGenerateCreativeRequest.templateType,
                                    templateId = templateGenerateCreativeRequest.templateExtId,
                                    name = templateGenerateCreativeRequest.name,
                                    metaData = solrTemplate.metaData,
                                    variables = inCurrentTemplateTemplateVariables.map { inCurrentTemplateTemplateVariable ->
                                        var templateVariable = templateVariables.firstOrNull { it.template.templateId == templateGenerateCreativeRequest.templateId
                                                && it.templateVariableId == inCurrentTemplateTemplateVariable.templateVariableId }

                                        if(templateVariable == null) {
                                            templateVariable = templateVariables.first { it.template.templateId == templateGenerateCreativeRequest.templateId &&
                                                it.variableSource?.sourceFieldId == inCurrentTemplateTemplateVariable.templateVariableId.toString() }
                                        }

                                        TemplateVariableInGenerateCreativeMessage(
                                                id = templateVariable.templateVariableExtId,
                                                value = inCurrentTemplateTemplateVariable.value!!)
                                    })
                        })
        ))
    }
}
