package com.betriebsratkanzlei.legalconsenthub_middleware.signature

import com.betriebsratkanzlei.legalconsenthub_middleware_api.api.SignatureApi
import com.betriebsratkanzlei.legalconsenthub_middleware_api.model.VerifySignatureResponseDto
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty
import org.springframework.http.ResponseEntity
import org.springframework.web.bind.annotation.RestController
import org.springframework.web.multipart.MultipartFile

@RestController
@ConditionalOnProperty(name = ["dummy.mode.enabled"], havingValue = "false", matchIfMissing = true)
class SignatureController(
    private val signatureService: SignatureService
) : SignatureApi {

    override fun signPdfHash(
        document: MultipartFile,
        certificateId: String,
        hashAlgorithm: String
    ): ResponseEntity<String> {
        val signature = signatureService.signPdfHash(
            document = document.bytes,
            certificateId = certificateId,
            hashAlgorithm = hashAlgorithm
        )
        return ResponseEntity.ok(signature)
    }

    override fun verifySignature(
        document: MultipartFile,
        signature: String,
        certificateId: String?,
        hashAlgorithm: String
    ): ResponseEntity<VerifySignatureResponseDto> {
        val verificationResult = signatureService.verifySignature(
            document = document.bytes,
            signature = signature,
            certificateId = certificateId,
            hashAlgorithm = hashAlgorithm
        )
        return ResponseEntity.ok(verificationResult)
    }
}
