package org.keycloak.operator.crds.v2beta1.deployment.spec;

import java.util.Map;

import org.keycloak.operator.crds.v2beta1.CRDUtils;
import org.keycloak.operator.crds.v2beta1.deployment.Keycloak;
import org.keycloak.operator.crds.v2beta1.deployment.KeycloakSpec;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonPropertyDescription;
import io.fabric8.generator.annotation.Default;
import io.sundr.builder.annotations.Buildable;

@JsonInclude(JsonInclude.Include.NON_NULL)
@Buildable(editableEnabled = false, builderPackage = "io.fabric8.kubernetes.api.builder")
public class ServiceMonitorSpec {

    public static final String DEFAULT_INTERVAL = "30s";
    public static final String DEFAULT_SCRAPE_TIMEOUT = "10s";

    @JsonPropertyDescription("Enables or disables the creation of the ServiceMonitor.")
    @Default("true")
    private boolean enabled = true;

    @JsonPropertyDescription("Interval at which metrics should be scraped")
    @Default(DEFAULT_INTERVAL)
    private String interval = DEFAULT_INTERVAL;

    @JsonPropertyDescription("Timeout after which the scrape is ended")
    @Default(DEFAULT_SCRAPE_TIMEOUT)
    private String scrapeTimeout = DEFAULT_SCRAPE_TIMEOUT;

    @JsonPropertyDescription("Annotations to be appended to the Service object")
    private Map<String, String> annotations;

    @JsonPropertyDescription("Labels to be appended to the Service object")
    private Map<String, String> labels;

    public boolean isEnabled() {
        return enabled;
    }

    public void setEnabled(boolean enabled) {
        this.enabled = enabled;
    }

    public String getInterval() {
        return interval;
    }

    public void setInterval(String interval) {
        this.interval = interval;
    }

    public String getScrapeTimeout() {
        return scrapeTimeout;
    }

    public void setScrapeTimeout(String scrapeTimeout) {
        this.scrapeTimeout = scrapeTimeout;
    }

    public Map<String, String> getAnnotations() {
        return annotations;
    }

    public void setAnnotations(Map<String, String> annotations) {
        this.annotations = annotations;
    }

    public Map<String, String> getLabels() {
        return labels;
    }

    public void setLabels(Map<String, String> labels) {
        this.labels = labels;
    }

    public static ServiceMonitorSpec get(Keycloak keycloak) {
        return CRDUtils.keycloakSpecOf(keycloak)
              .map(KeycloakSpec::getServiceMonitorSpec)
              .orElse(new ServiceMonitorSpec());
    }
}
