package com.betriebsratkanzlei.legalconsenthub.application_form

import com.betriebsratkanzlei.legalconsenthub_api.api.ApplicationFormApi
import com.betriebsratkanzlei.legalconsenthub_api.model.ApplicationFormDto
import com.betriebsratkanzlei.legalconsenthub_api.model.CreateApplicationFormDto
import com.betriebsratkanzlei.legalconsenthub_api.model.PagedApplicationFormDto
import org.springframework.core.io.ByteArrayResource
import org.springframework.core.io.Resource
import org.springframework.http.HttpHeaders
import org.springframework.http.MediaType
import org.springframework.http.ResponseEntity
import org.springframework.web.bind.annotation.RestController
import java.util.UUID

@RestController
class ApplicationFormController(
    val applicationFormService: ApplicationFormService,
    val pagedApplicationFormMapper: PagedApplicationFormMapper,
    val applicationFormMapper: ApplicationFormMapper,
    val applicationFormFormatService: ApplicationFormFormatService
) : ApplicationFormApi {

    override fun createApplicationForm(createApplicationFormDto: CreateApplicationFormDto): ResponseEntity<ApplicationFormDto> {
        val updatedCreateApplicationFormDto = createApplicationFormDto.copy(isTemplate = false)
        return ResponseEntity.ok(
            applicationFormMapper.toApplicationFormDto(
                applicationFormService.createApplicationForm(updatedCreateApplicationFormDto)
            )
        )
    }

    override fun getAllApplicationForms(organizationId: String?): ResponseEntity<PagedApplicationFormDto> {
        return ResponseEntity.ok(
            pagedApplicationFormMapper.toPagedApplicationFormDto(
                applicationFormService.getApplicationForms(organizationId)
            )
        )
    }

    override fun getApplicationFormById(id: UUID): ResponseEntity<ApplicationFormDto> {
        return ResponseEntity.ok(
            applicationFormMapper.toApplicationFormDto(
                applicationFormService.getApplicationFormById(id)
            )
        )
    }

    override fun getApplicationFormHtml(id: UUID): ResponseEntity<String> {
        val applicationForm = applicationFormService.getApplicationFormById(id)
        return ResponseEntity.ok(
            applicationFormFormatService.generateHtml(applicationForm)
        )
    }

    override fun getApplicationFormPdf(id: UUID): ResponseEntity<Resource> {
        val applicationForm = applicationFormService.getApplicationFormById(id)
        val pdfBytes = applicationFormFormatService.generatePdf(applicationForm)
        val resource = ByteArrayResource(pdfBytes)
        return ResponseEntity.ok()
            .header(HttpHeaders.CONTENT_DISPOSITION, "inline; filename=\"form-$id.pdf\"")
            .contentType(MediaType.APPLICATION_PDF)
            .body(resource)
    }

    override fun updateApplicationForm(
        id: UUID,
        applicationFormDto: ApplicationFormDto
    ): ResponseEntity<ApplicationFormDto> {
        return ResponseEntity.ok(
            applicationFormMapper.toApplicationFormDto(
                applicationFormService.updateApplicationForm(applicationFormDto)
            )
        )
    }

    override fun deleteApplicationForm(id: UUID): ResponseEntity<Unit> {
        applicationFormService.deleteApplicationFormByID(id)
        return ResponseEntity.noContent().build()
    }

    override fun submitApplicationForm(id: UUID): ResponseEntity<ApplicationFormDto> {
        return ResponseEntity.ok(
            applicationFormMapper.toApplicationFormDto(
                applicationFormService.submitApplicationForm(id)
            )
        )
    }
}
