package com.iqser.red.service.persistence.management.v1.processor.service;

import jakarta.transaction.Transactional;

import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import com.iqser.red.service.persistence.management.v1.processor.client.pdftronredactionservice.PDFTronClient;
import com.iqser.red.service.persistence.management.v1.processor.entity.configuration.DigitalSignatureEntity;
import com.iqser.red.service.persistence.management.v1.processor.exception.BadRequestException;
import com.iqser.red.service.persistence.management.v1.processor.exception.NotFoundException;
import com.iqser.red.service.persistence.management.v1.processor.service.persistence.repository.DigitalSignatureRepository;
import com.knecon.fforesight.databasetenantcommons.providers.utils.MagicConverter;
import com.iqser.red.service.persistence.service.v1.api.shared.model.dossiertemplate.configuration.DigitalSignature;
import com.iqser.red.service.persistence.service.v1.api.shared.model.dossiertemplate.configuration.DigitalSignatureType;
import com.knecon.fforesight.tenantcommons.EncryptionDecryptionService;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@Service
@RequiredArgsConstructor
public class DigitalSignatureService {

    private final EncryptionDecryptionService encryptionDecryptionService;
    private final DigitalSignatureRepository digitalSignatureRepository;
    private final PDFTronClient pdfTronRedactionClient;
    private final DigitalSignatureTypeService digitalSignatureTypeService;


    public void deleteDigitalSignature() {

        digitalSignatureRepository.deleteById(DigitalSignatureEntity.ID);
        digitalSignatureTypeService.deleteDigitalSignatureType();
    }


    public DigitalSignatureEntity getDigitalSignature() {

        return digitalSignatureRepository.findById(DigitalSignatureEntity.ID).map(digitalSignature -> {
            DigitalSignatureEntity result = new DigitalSignatureEntity();
            BeanUtils.copyProperties(digitalSignature, result);
            result.setPrivateKey(encryptionDecryptionService.decrypt(digitalSignature.getPrivateKey()));
            result.setPassword(encryptionDecryptionService.decrypt(digitalSignature.getPassword()));
            return result;
        }).orElseThrow(() -> new NotFoundException("Digital Signature Not found"));
    }


    public DigitalSignatureEntity saveDigitalSignature(DigitalSignatureEntity digitalSignature) {

        try {
            pdfTronRedactionClient.testDigitalCurrentSignature(MagicConverter.convert(digitalSignature, DigitalSignature.class));
        } catch (Exception e) {
            throw new BadRequestException("Failed to test digital signature");
        }

        digitalSignature.setPrivateKey(encryptionDecryptionService.encrypt(digitalSignature.getPrivateKey()));
        digitalSignature.setPassword(encryptionDecryptionService.encrypt(digitalSignature.getPassword()));
        var result = digitalSignatureRepository.save(digitalSignature);

        digitalSignatureTypeService.setActiveDigitalSignatureType(DigitalSignatureType.CERTIFICATE);
        return result;
    }


    @Transactional
    public void updateDigitalSignature(DigitalSignatureEntity digitalSignatureModel) {

        int updateCount = digitalSignatureRepository.updateDigitalSignature(DigitalSignatureEntity.ID,
                digitalSignatureModel.getReason(),
                digitalSignatureModel.getLocation(),
                digitalSignatureModel.getContactInfo(),
                digitalSignatureModel.getCertificateName());
        if (updateCount == 0) {
            throw new NotFoundException("Digital Signature Not found");
        }
        digitalSignatureTypeService.setActiveDigitalSignatureType(DigitalSignatureType.CERTIFICATE);
    }

}
