package com.securityc4po.api.pentest.comment

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 com.securityc4po.api.pentest.PentestService
import org.springframework.http.ResponseEntity
import org.springframework.http.ResponseEntity.noContent
import org.springframework.web.bind.annotation.*
import reactor.core.publisher.Mono
import reactor.kotlin.core.publisher.switchIfEmpty

@RestController
@RequestMapping("/pentests")
@CrossOrigin(
    origins = [],
    allowCredentials = "false",
    allowedHeaders = ["*"],
    methods = [RequestMethod.GET, RequestMethod.DELETE, RequestMethod.POST, RequestMethod.PATCH]
)
@SuppressFBWarnings(BC_BAD_CAST_TO_ABSTRACT_COLLECTION)
class CommentController(private val pentestService: PentestService, private val commentService: CommentService) {

    var logger = getLoggerFor<CommentController>()

    @GetMapping("/{pentestId}/comments")
    fun getComments(@PathVariable(value = "pentestId") pentestId: String): Mono<ResponseEntity<List<ResponseBody>>> {
        return this.pentestService.getCommentIdsByPentestId(pentestId).flatMap { commentIds: List<String> ->
            this.commentService.getCommentsByIds(commentIds).map { commentList ->
                commentList.map { it.toCommentResponseBody() }
            }
        }.map {
            if (it.isEmpty()) noContent().build()
            else ResponseEntity.ok(it)
        }
    }

    @GetMapping("/{commentId}/comment")
    fun getComment(@PathVariable(value = "commentId") commentId: String):Mono<ResponseEntity<ResponseBody>> {
        return this.commentService.getCommentById(commentId).map {
            ResponseEntity.ok().body(it.toCommentResponseBody())
        }
    }

    @PostMapping("/{pentestId}/comment")
    fun saveComment(
        @PathVariable(value = "pentestId") pentestId: String,
        @RequestBody body: CommentRequestBody
    ): Mono<ResponseEntity<ResponseBody>> {
        return this.commentService.saveComment(pentestId, body).map {
            ResponseEntity.accepted().body(it.toCommentResponseBody())
        }
    }

    @PatchMapping("/{commentId}/comment")
    fun updateComment(
        @PathVariable(value = "commentId") commentId: String,
        @RequestBody body: CommentRequestBody
    ): Mono<ResponseEntity<ResponseBody>> {
        return this.commentService.updateComment(commentId, body).map {
            ResponseEntity.accepted().body(it.toCommentResponseBody())
        }
    }

    @DeleteMapping("/{pentestId}/comment/{commentId}")
    fun deleteComment(
        @PathVariable(value = "pentestId") pentestId: String,
        @PathVariable(value = "commentId") commentId: String
    ): Mono<ResponseEntity<ResponseBody>> {
        return this.commentService.deleteCommentByPentestAndCommentId(pentestId, commentId).map {
            ResponseEntity.ok().body(it.toCommentDeleteResponseBody())
        }.switchIfEmpty {
            Mono.just(noContent().build<ResponseBody>())
        }
    }
}