package com.betriebsratkanzlei.legalconsenthub_middleware.signature

import com.betriebsratkanzlei.legalconsenthub_middleware_api.api.SignatureApi
import com.betriebsratkanzlei.legalconsenthub_middleware_api.model.VerifySignatureResponseDto
import com.betriebsratkanzlei.legalconsenthub_middleware_api.model.CertificateDto
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
import java.time.LocalDateTime
import java.util.*

@RestController
@ConditionalOnProperty(name = ["dummy.mode.enabled"], havingValue = "true")
class DummySignatureController : SignatureApi {

    override fun signPdfHash(
        document: MultipartFile,
        certificateId: String,
        hashAlgorithm: String
    ): ResponseEntity<String> {
        // Always return a successful dummy signature
        val dummySignature = "dummySignature_${certificateId}_${System.currentTimeMillis()}"
        val encodedSignature = Base64.getEncoder().encodeToString(dummySignature.toByteArray())
        return ResponseEntity.ok(encodedSignature)
    }

    override fun verifySignature(
        document: MultipartFile,
        signature: String,
        certificateId: String?,
        hashAlgorithm: String
    ): ResponseEntity<VerifySignatureResponseDto> {
        // Always return successful verification with dummy certificate
        val dummyCertificate = CertificateDto(
            id = certificateId ?: "01",
            subject = "CN=John Doe, O=Example Company, L=Berlin, C=DE",
            issuer = "CN=Dummy CA, O=Dummy Corp, C=DE",
            validFrom = LocalDateTime.now().minusYears(1),
            validTo = LocalDateTime.now().plusYears(2),
            keyUsage = listOf("digitalSignature", "keyEncipherment", "nonRepudiation"),
            fingerprint = "AA:BB:CC:DD:EE:FF:00:11:22:33:44:55:66:77:88:99:AA:BB:CC:DD"
        )

        val successfulResponse = VerifySignatureResponseDto(
            isValid = true,
            certificateInfo = dummyCertificate,
            verificationDetails = "DUMMY: Signature verified successfully using ${hashAlgorithm} algorithm"
        )

        return ResponseEntity.ok(successfulResponse)
    }
} 