package com.qmth.boot.mybatis.config;

import com.qmth.boot.core.constant.CoreConstant;
import com.qmth.boot.core.logger.constant.LoggerConstant;
import org.hibernate.validator.constraints.time.DurationMin;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import org.springframework.validation.annotation.Validated;

import javax.validation.constraints.Min;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.time.Duration;

@Component
@ConfigurationProperties(prefix = CoreConstant.CONFIG_PREFIX + ".datasource")
@Validated
public class DataSourceProperties {

    @NotBlank
    private String driverClassName = "com.mysql.cj.jdbc.Driver";

    @NotBlank
    private String url;

    @NotBlank
    private String username;

    @NotBlank
    private String password;

    @Min(0)
    private int minIdle = 0;

    @Min(1)
    private int maxPoolSize = 10;

    @NotNull
    @DurationMin(seconds = 60)
    private Duration idleTimeout = Duration.ofMinutes(10);

    @NotNull
    @DurationMin(seconds = 5)
    private Duration connectionTimeout = Duration.ofSeconds(30);

    @NotNull
    @DurationMin(millis = 0)
    private Duration maxLifetime = Duration.ofHours(7);

    @NotNull
    private String logLevel = LoggerConstant.DEFAULT_LEVEL;

    public String getDriverClassName() {
        return driverClassName;
    }

    public void setDriverClassName(String driverClassName) {
        this.driverClassName = driverClassName;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public int getMinIdle() {
        return minIdle;
    }

    public void setMinIdle(int minIdle) {
        this.minIdle = minIdle;
    }

    public int getMaxPoolSize() {
        return maxPoolSize;
    }

    public void setMaxPoolSize(int maxPoolSize) {
        this.maxPoolSize = maxPoolSize;
    }

    public Duration getIdleTimeout() {
        return idleTimeout;
    }

    public void setIdleTimeout(Duration idleTimeout) {
        this.idleTimeout = idleTimeout;
    }

    public Duration getConnectionTimeout() {
        return connectionTimeout;
    }

    public void setConnectionTimeout(Duration connectionTimeout) {
        this.connectionTimeout = connectionTimeout;
    }

    public Duration getMaxLifetime() {
        return maxLifetime;
    }

    public void setMaxLifetime(Duration maxLifetime) {
        this.maxLifetime = maxLifetime;
    }

    public String getLogLevel() {
        return logLevel;
    }

    public void setLogLevel(String logLevel) {
        this.logLevel = logLevel;
    }
}
