package net.siegeln.cameleer.saas.provisioning;

import com.github.dockerjava.core.DefaultDockerClientConfig;
import com.github.dockerjava.core.DockerClientConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

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

@Configuration
@EnableConfigurationProperties(ProvisioningProperties.class)
public class TenantProvisionerAutoConfig {
    private static final Logger log = LoggerFactory.getLogger(TenantProvisionerAutoConfig.class);

    @Bean
    TenantProvisioner tenantProvisioner(ProvisioningProperties props) {
        if (Files.exists(Path.of("/var/run/docker.sock"))) {
            log.info("Docker socket detected — enabling Docker tenant provisioner");
            DockerClientConfig config = DefaultDockerClientConfig.createDefaultConfigBuilder()
                .withDockerHost("unix:///var/run/docker.sock")
                .build();
            return new DockerTenantProvisioner(config, props);
        }
        log.info("No Docker socket — tenant provisioning disabled");
        return new DisabledTenantProvisioner();
    }
}
