package net.siegeln.cameleer.saas.provisioning;

import net.siegeln.cameleer.saas.certificate.CertificateManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.nio.file.Files;
import java.nio.file.Path;

@Configuration
public class CertificateManagerAutoConfig {

    private static final Logger log = LoggerFactory.getLogger(CertificateManagerAutoConfig.class);

    @Bean
    CertificateManager certificateManager(
            @Value("${cameleer.certs.path:/certs}") String certsPath) {
        Path path = Path.of(certsPath);
        if (Files.isDirectory(path)) {
            log.info("Certs directory found at {} — enabling Docker certificate manager", certsPath);
            return new DockerCertificateManager(path);
        }
        log.info("No certs directory at {} — certificate management disabled", certsPath);
        return new DisabledCertificateManager();
    }
}
