package go.kr.project.api.internal.gpki;

import go.kr.project.api.config.properties.VmisProperties;
import go.kr.project.api.internal.util.GpkiCryptoUtil;

/**
 * Real GPKI service backed by native GPKI JNI via legacy NewGpkiUtil wrapper.
 * Uses YAML-configured paths and options in {@link VmisProperties.GpkiProps}.
 */
public class RealGpkiService implements GpkiService {

    private final VmisProperties props;
    private final GpkiCryptoUtil crypto;

    public RealGpkiService(VmisProperties props) {
        this.props = props;
        try {
            this.crypto = GpkiCryptoUtil.from(props.getGpki());
        } catch (Exception e) {
            throw new IllegalStateException("Failed to initialize GPKI (JNI) util. Check YAML paths/passwords and license.", e);
        }
    }

    @Override
    public String encrypt(String plain) throws Exception {
        String charset = props.getGpki().getCharset();
        String targetId = props.getGpki().getTargetServerId();
        return crypto.encryptToBase64(plain, targetId, charset);
    }

    @Override
    public String decrypt(String cipher) throws Exception {
        String charset = props.getGpki().getCharset();
        return crypto.decryptFromBase64(cipher, charset);
    }

    @Override
    public boolean isEnabled() {
        return true;
    }
}
