package dp.kotul.templategroupsbackend.helper

import dp.kotul.solrapimodel.SolrApiSearchParameters
import dp.kotul.solrapimodel.solrsearch.AssetType
import dp.kotul.solrapimodel.solrsearch.AssetType.TEMPLATE
import dp.kotul.solrapimodel.solrsearch.AssetType.TEMPLATE_GROUP
import dp.kotul.solrapimodel.solrsearch.SolrUtils
import dp.kotul.starter.filter.RequestCorrelation
import dp.kotul.starter.service.AuthorizationService
import dp.kotul.templategroupsbackend.client.SolrApiClient
import dp.kotul.templategroupsbackend.model.entity.Template
import dp.kotul.templategroupsbackend.model.solrsearch.DEFAULT_QUERY_VALUE
import dp.kotul.templategroupsbackend.model.solrsearch.DEFAULT_SORT
import dp.kotul.templategroupsbackend.model.solrsearch.DEFAULT_SORT_BY
import dp.kotul.templategroupsbackend.model.solrsearch.DEFAULT_START
import dp.kotul.templategroupsbackend.model.solrsearch.SolrSearchRequest
import dp.kotul.templategroupsbackend.model.solrsearch.tamplategroup.TemplateGroupSolrResponse
import dp.kotul.templategroupsbackend.model.solrsearch.template.TemplateSolrResponse
import org.springframework.stereotype.Component

const val SOLR_TEMPLATES_DEFAULT_ROWS = 1000

interface SolrSearchHelper {
    fun getSolrTemplateGroupsResponse(laeId: Int, solrSearchRequest: SolrSearchRequest): TemplateGroupSolrResponse?
    fun getSolrTemplatesResponse(laeId: Int, solrSearchRequest: SolrSearchRequest): TemplateSolrResponse?
    fun getSolrTemplatesResponse(laeId: Int, templates: List<Template>): TemplateSolrResponse?
    fun getSolrTemplatesResponse(accessToken: String, correlationId: String, laeId: Int, templates: List<Template>): TemplateSolrResponse?
}

@Component
class SolrSearchHelperImpl(
        val authService: AuthorizationService,
        val solrApiClient: SolrApiClient
) : SolrSearchHelper {

    override fun getSolrTemplateGroupsResponse(laeId: Int, solrSearchRequest: SolrSearchRequest): TemplateGroupSolrResponse? {
        val accessToken = authService.getValidAuthorizationHeader()
        val correlationId = RequestCorrelation.getId()

        return solrApiClient.getSolrTemplateGroupsResponse(accessToken, correlationId, laeId,
                SolrApiSearchParameters(
                        q = "\"${solrSearchRequest.searchText}\"",
                        fq = "+assetType:${TEMPLATE_GROUP.value}",
                        fl = null,
                        sort = listOf("${solrSearchRequest.sortBy} ${solrSearchRequest.sort}"),
                        start = solrSearchRequest.start,
                        rows = solrSearchRequest.rows))
    }

    override fun getSolrTemplatesResponse(laeId: Int, solrSearchRequest: SolrSearchRequest): TemplateSolrResponse? {
        val accessToken = authService.getValidAuthorizationHeader()
        val correlationId = RequestCorrelation.getId()

        return solrApiClient.getSolrTemplatesResponse(accessToken, correlationId, laeId,
                SolrApiSearchParameters(
                        q = "\"${solrSearchRequest.searchText}\"",
                        fq = "+assetType:${TEMPLATE.value}",
                        fl = null,
                        sort = listOf("${solrSearchRequest.sortBy} ${solrSearchRequest.sort}"),
                        start = solrSearchRequest.start,
                        rows = solrSearchRequest.rows))
    }

    override fun getSolrTemplatesResponse(laeId: Int, templates: List<Template>): TemplateSolrResponse? {
        val accessToken = authService.getValidAuthorizationHeader()
        val correlationId = RequestCorrelation.getId()

        return getSolrTemplatesResponse(accessToken, correlationId, laeId, templates)
    }

    override fun getSolrTemplatesResponse(accessToken: String, correlationId: String, laeId: Int, templates: List<Template>): TemplateSolrResponse? {

        return solrApiClient.getSolrTemplatesResponse(accessToken, correlationId, laeId,
                SolrApiSearchParameters(
                        q = "\"$DEFAULT_QUERY_VALUE\"",
                        fq = templates.joinToString { "id:\"${SolrUtils.getUniqueSolrDocumentId(it.templateExtId, AssetType.TEMPLATE, it.templateType.name, laeId)}\"" },
                        fl = null,
                        sort = listOf("$DEFAULT_SORT_BY $DEFAULT_SORT"),
                        start = DEFAULT_START,
                        rows = SOLR_TEMPLATES_DEFAULT_ROWS))
    }
}
