package dp.kotul.templategroupsbackend.controller

import dp.kotul.starter.global.DefaultApiResponses
import dp.kotul.templategroupsbackend.facade.TemplateFacade
import dp.kotul.templategroupsbackend.model.solrsearch.SolrSearchRequest
import dp.kotul.templategroupsbackend.model.solrsearch.template.TemplateSolrResponse
import io.swagger.annotations.Api
import io.swagger.annotations.ApiOperation
import io.swagger.annotations.ApiParam
import org.springframework.http.HttpStatus.NOT_FOUND
import org.springframework.http.HttpStatus.OK
import org.springframework.http.MediaType
import org.springframework.http.ResponseEntity
import org.springframework.web.bind.annotation.PostMapping
import org.springframework.web.bind.annotation.RequestBody
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController

@Api(tags = ["Templates"], description = "Provides API for templates operations")
@DefaultApiResponses
@RestController
@RequestMapping(produces = [MediaType.APPLICATION_JSON_VALUE])
class TemplateController(val templateFacade: TemplateFacade) {

    @ApiOperation(value = "Get templates solr", notes = "Returns templates with predefined search parameters from solr collection for company of current user")
    @PostMapping("/templates")
    fun getTemplatesSolr(
        @ApiParam(value = "Solr Search JSON object", required = true) @RequestBody solrSearchRequest: SolrSearchRequest
    ): ResponseEntity<TemplateSolrResponse?> {
        return when (val templatesSearchResponse = templateFacade.getTemplatesSolr(solrSearchRequest)) {
            null -> ResponseEntity(NOT_FOUND)
            else -> ResponseEntity(templatesSearchResponse, OK)
        }
    }
}
