package app

import (
	"fmt"
	"strconv"

	"coopcloud.tech/abra/pkg/envfile"
	"coopcloud.tech/abra/pkg/log"
	composetypes "github.com/docker/cli/cli/compose/types"
)

// SetRecipeLabel adds the label 'coop-cloud.${STACK_NAME}.recipe=${RECIPE}' to the app container
// to signal which recipe is connected to the deployed app
func SetRecipeLabel(compose *composetypes.Config, stackName string, recipe string) {
	for _, service := range compose.Services {
		if service.Name == "app" {
			log.Debugf("set recipe label 'coop-cloud.%s.recipe' to %s for %s", stackName, recipe, stackName)
			labelKey := fmt.Sprintf("coop-cloud.%s.recipe", stackName)
			service.Deploy.Labels[labelKey] = recipe
		}
	}
}

// SetChaosLabel adds the label 'coop-cloud.${STACK_NAME}.chaos=true/false' to the app container
// to signal if the app is deployed in chaos mode
func SetChaosLabel(compose *composetypes.Config, stackName string, chaos bool) {
	for _, service := range compose.Services {
		if service.Name == "app" {
			log.Debugf("set label 'coop-cloud.%s.chaos' to %v for %s", stackName, chaos, stackName)
			labelKey := fmt.Sprintf("coop-cloud.%s.chaos", stackName)
			service.Deploy.Labels[labelKey] = strconv.FormatBool(chaos)
		}
	}
}

// SetChaosVersionLabel adds the label 'coop-cloud.${STACK_NAME}.chaos-version=$(GIT_COMMIT)' to the app container
func SetChaosVersionLabel(compose *composetypes.Config, stackName string, chaosVersion string) {
	for _, service := range compose.Services {
		if service.Name == "app" {
			log.Debugf("set label 'coop-cloud.%s.chaos-version' to %v for %s", stackName, chaosVersion, stackName)
			labelKey := fmt.Sprintf("coop-cloud.%s.chaos-version", stackName)
			service.Deploy.Labels[labelKey] = chaosVersion
		}
	}
}

// SetUpdateLabel adds env ENABLE_AUTO_UPDATE as label to enable/disable the
// auto update process for this app. The default if this variable is not set is to disable
// the auto update process.
func SetUpdateLabel(compose *composetypes.Config, stackName string, appEnv envfile.AppEnv) {
	for _, service := range compose.Services {
		if service.Name == "app" {
			enable_auto_update, exists := appEnv["ENABLE_AUTO_UPDATE"]
			if !exists {
				enable_auto_update = "false"
			}
			log.Debugf("set label 'coop-cloud.%s.autoupdate' to %s for %s", stackName, enable_auto_update, stackName)
			labelKey := fmt.Sprintf("coop-cloud.%s.autoupdate", stackName)
			service.Deploy.Labels[labelKey] = enable_auto_update
		}
	}
}

// GetLabel reads docker labels in the format of "coop-cloud.${STACK_NAME}.${LABEL}" from the local compose files
func GetLabel(compose *composetypes.Config, stackName string, label string) string {
	for _, service := range compose.Services {
		if service.Name == "app" {
			labelKey := fmt.Sprintf("coop-cloud.%s.%s", stackName, label)
			log.Debugf("get label '%s'", labelKey)
			if labelValue, ok := service.Deploy.Labels[labelKey]; ok {
				return labelValue
			}
		}
	}
	log.Debugf("no %s label found for %s", label, stackName)
	return ""
}

// GetTimeoutFromLabel reads the timeout value from docker label "coop-cloud.${STACK_NAME}.TIMEOUT" and returns 50 as default value
func GetTimeoutFromLabel(compose *composetypes.Config, stackName string) (int, error) {
	timeout := 50 // Default Timeout
	var err error = nil
	if timeoutLabel := GetLabel(compose, stackName, "timeout"); timeoutLabel != "" {
		log.Debugf("timeout label: %s", timeoutLabel)
		timeout, err = strconv.Atoi(timeoutLabel)
	}
	return timeout, err
}