package com.securityc4po.api.pentest

import com.securityc4po.api.configuration.BC_BAD_CAST_TO_ABSTRACT_COLLECTION
import com.securityc4po.api.extensions.getLoggerFor
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings
import com.securityc4po.api.ResponseBody
import org.springframework.http.ResponseEntity
import org.springframework.http.ResponseEntity.noContent
import org.springframework.web.bind.annotation.*
import reactor.core.publisher.Mono

@RestController
@RequestMapping("/pentests")
@CrossOrigin(
    origins = [],
    allowCredentials = "false",
    allowedHeaders = ["*"],
    methods = [RequestMethod.GET]
)

@SuppressFBWarnings(BC_BAD_CAST_TO_ABSTRACT_COLLECTION)
class PentestController(private val pentestService: PentestService) {

    var logger = getLoggerFor<PentestController>()

    @GetMapping
    fun getPentestsByProjectIdAndCategory(
        @RequestParam("projectId") projectId: String,
        @RequestParam("category") category: String
    ): Mono<ResponseEntity<List<ResponseBody>>> {
        return pentestService.getPentests(projectId, PentestCategory.valueOf(category)).map { pentestList ->
            pentestList.map {
                it.toPentestResponseBody()
            }
        }.map {
            if (it.isEmpty()) noContent().build()
            else ResponseEntity.ok(it)
        }
    }
}
