package com.betriebsratkanzlei.legalconsenthub.comment

import com.betriebsratkanzlei.legalconsenthub_api.api.CommentApi
import com.betriebsratkanzlei.legalconsenthub_api.model.CommentDto
import com.betriebsratkanzlei.legalconsenthub_api.model.CreateCommentDto
import com.betriebsratkanzlei.legalconsenthub_api.model.PagedCommentDto
import org.springframework.http.ResponseEntity
import org.springframework.security.access.prepost.PreAuthorize
import org.springframework.web.bind.annotation.RestController
import java.util.UUID

@RestController
class CommentController(
    val commentService: CommentService,
    val commentMapper: CommentMapper,
    val pagedCommentMapper: PagedCommentMapper,
) : CommentApi {
    @PreAuthorize(
        "hasAnyRole('CHIEF_EXECUTIVE_OFFICER', 'BUSINESS_DEPARTMENT', 'IT_DEPARTMENT', 'HUMAN_RESOURCES', 'HEAD_OF_WORKS_COUNCIL', 'WORKS_COUNCIL', 'EMPLOYEE')",
    )
    override fun createComment(
        applicationFormId: UUID,
        formElementId: UUID,
        createCommentDto: CreateCommentDto,
    ): ResponseEntity<CommentDto> =
        ResponseEntity.ok(
            commentMapper.toCommentDto(
                commentService.createComment(applicationFormId, formElementId, createCommentDto),
            ),
        )

    @PreAuthorize(
        "hasAnyRole('CHIEF_EXECUTIVE_OFFICER', 'BUSINESS_DEPARTMENT', 'IT_DEPARTMENT', 'HUMAN_RESOURCES', 'HEAD_OF_WORKS_COUNCIL', 'WORKS_COUNCIL', 'EMPLOYEE')",
    )
    override fun getCommentsByApplicationFormId(applicationFormId: UUID): ResponseEntity<PagedCommentDto> =
        ResponseEntity.ok(
            pagedCommentMapper.toPagedCommentDto(
                commentService.getComments(applicationFormId),
            ),
        )

    @PreAuthorize(
        "hasAnyRole('CHIEF_EXECUTIVE_OFFICER', 'BUSINESS_DEPARTMENT', 'IT_DEPARTMENT', 'HUMAN_RESOURCES', 'HEAD_OF_WORKS_COUNCIL', 'WORKS_COUNCIL', 'EMPLOYEE')",
    )
    override fun updateComment(
        id: UUID,
        commentDto: CommentDto,
    ): ResponseEntity<CommentDto> =
        ResponseEntity.ok(
            commentMapper.toCommentDto(
                commentService.updateComment(commentDto),
            ),
        )

    @PreAuthorize(
        "hasAnyRole('CHIEF_EXECUTIVE_OFFICER', 'BUSINESS_DEPARTMENT', 'IT_DEPARTMENT', 'HUMAN_RESOURCES', 'HEAD_OF_WORKS_COUNCIL', 'WORKS_COUNCIL')",
    )
    override fun deleteComment(id: UUID): ResponseEntity<Unit> {
        commentService.deleteCommentByID(id)
        return ResponseEntity.noContent().build()
    }
}
