package com.linkedin.metadata.restli;

import io.ebean.datasource.DataSourceConfig;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

import java.util.HashMap;
import java.util.Map;

import static com.linkedin.gms.factory.common.LocalEbeanServerConfigFactory.getListenerToTrackCounts;

@Configuration
public class EbeanServerConfig {
    @Value("${ebean.username}")
    private String ebeanDatasourceUsername;

    @Value("${ebean.password}")
    private String ebeanDatasourcePassword;

    @Value("${ebean.driver}")
    private String ebeanDatasourceDriver;

    @Value("${ebean.minConnections:1}")
    private Integer ebeanMinConnections;

    @Value("${ebean.maxInactiveTimeSeconds:120}")
    private Integer ebeanMaxInactiveTimeSecs;

    @Value("${ebean.maxAgeMinutes:120}")
    private Integer ebeanMaxAgeMinutes;

    @Value("${ebean.leakTimeMinutes:15}")
    private Integer ebeanLeakTimeMinutes;

    @Value("${ebean.waitTimeoutMillis:1000}")
    private Integer ebeanWaitTimeoutMillis;

    @Value("${ebean.autoCreateDdl:false}")
    private Boolean ebeanAutoCreate;

    @Value("${ebean.postgresUseIamAuth:false}")
    private Boolean postgresUseIamAuth;


    @Bean("ebeanDataSourceConfig")
    @Primary
    public DataSourceConfig buildDataSourceConfig(
            @Value("${ebean.url}") String dataSourceUrl,
            @Qualifier("parseqEngineThreads") int ebeanMaxConnections
    ) {
        DataSourceConfig dataSourceConfig = new DataSourceConfig();
        dataSourceConfig.setUsername(ebeanDatasourceUsername);
        dataSourceConfig.setPassword(ebeanDatasourcePassword);
        dataSourceConfig.setUrl(dataSourceUrl);
        dataSourceConfig.setDriver(ebeanDatasourceDriver);
        dataSourceConfig.setMinConnections(ebeanMinConnections);
        dataSourceConfig.setMaxConnections(ebeanMaxConnections);
        dataSourceConfig.setMaxInactiveTimeSecs(ebeanMaxInactiveTimeSecs);
        dataSourceConfig.setMaxAgeMinutes(ebeanMaxAgeMinutes);
        dataSourceConfig.setLeakTimeMinutes(ebeanLeakTimeMinutes);
        dataSourceConfig.setWaitTimeoutMillis(ebeanWaitTimeoutMillis);
        dataSourceConfig.setListener(getListenerToTrackCounts("mce-consumer"));
        // Adding IAM auth access for AWS Postgres
        if (postgresUseIamAuth) {
            Map<String, String> custom = new HashMap<>();
            custom.put("wrapperPlugins", "iam");
            dataSourceConfig.setCustomProperties(custom);
        }
        return dataSourceConfig;
    }
}
