package com.betriebsratkanzlei.legalconsenthub.application_form_template

import com.betriebsratkanzlei.legalconsenthub.application_form.ApplicationFormMapper
import com.betriebsratkanzlei.legalconsenthub.application_form.PagedApplicationFormMapper
import com.betriebsratkanzlei.legalconsenthub_api.api.ApplicationFormTemplateApi
import com.betriebsratkanzlei.legalconsenthub_api.model.ApplicationFormDto
import com.betriebsratkanzlei.legalconsenthub_api.model.CreateApplicationFormDto
import com.betriebsratkanzlei.legalconsenthub_api.model.PagedApplicationFormDto
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 ApplicationFormTemplateController(
    val applicationFormTemplateService: ApplicationFormTemplateService,
    val pagedApplicationFormMapper: PagedApplicationFormMapper,
    val applicationFormMapper: ApplicationFormMapper,
) : ApplicationFormTemplateApi {
    @PreAuthorize(
        "hasAnyRole('CHIEF_EXECUTIVE_OFFICER', 'BUSINESS_DEPARTMENT', 'IT_DEPARTMENT', 'HUMAN_RESOURCES', 'HEAD_OF_WORKS_COUNCIL', 'WORKS_COUNCIL')",
    )
    override fun createApplicationFormTemplate(
        createApplicationFormDto: CreateApplicationFormDto,
    ): ResponseEntity<ApplicationFormDto> =
        ResponseEntity.ok(
            applicationFormMapper.toApplicationFormDto(
                applicationFormTemplateService.createApplicationFormTemplate(createApplicationFormDto),
            ),
        )

    @PreAuthorize(
        "hasAnyRole('CHIEF_EXECUTIVE_OFFICER', 'BUSINESS_DEPARTMENT', 'IT_DEPARTMENT', 'HUMAN_RESOURCES', 'HEAD_OF_WORKS_COUNCIL', 'WORKS_COUNCIL')",
    )
    override fun getAllApplicationFormTemplates(): ResponseEntity<PagedApplicationFormDto> =
        ResponseEntity.ok(
            pagedApplicationFormMapper.toPagedApplicationFormDto(
                applicationFormTemplateService.getApplicationFormTemplates(),
            ),
        )

    @PreAuthorize(
        "hasAnyRole('CHIEF_EXECUTIVE_OFFICER', 'BUSINESS_DEPARTMENT', 'IT_DEPARTMENT', 'HUMAN_RESOURCES', 'HEAD_OF_WORKS_COUNCIL', 'WORKS_COUNCIL')",
    )
    override fun getApplicationFormTemplateById(id: UUID): ResponseEntity<ApplicationFormDto> =
        ResponseEntity.ok(
            applicationFormMapper.toApplicationFormDto(
                applicationFormTemplateService.getApplicationFormTemplateById(id),
            ),
        )

    @PreAuthorize(
        "hasAnyRole('CHIEF_EXECUTIVE_OFFICER', 'BUSINESS_DEPARTMENT', 'IT_DEPARTMENT', 'HUMAN_RESOURCES', 'HEAD_OF_WORKS_COUNCIL', 'WORKS_COUNCIL')",
    )
    override fun updateApplicationFormTemplate(
        id: UUID,
        applicationFormDto: ApplicationFormDto,
    ): ResponseEntity<ApplicationFormDto> =
        ResponseEntity.ok(
            applicationFormMapper.toApplicationFormDto(
                applicationFormTemplateService.updateApplicationFormTemplate(applicationFormDto),
            ),
        )

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