package csi

import (
	"kube-forge/internal/config"
	"kube-forge/internal/helm_client"
	"kube-forge/internal/templates"
	"time"

	go_helm_client "github.com/mittwald/go-helm-client"
)

var HELM_REPOS = []config.RepoSettings{
	{
		Name: "kube-forge",
		URL:  "https://git.kvazaric.ru/api/v4/projects/41/packages/helm/stable",
	},
}

func getLonghornSpec() go_helm_client.ChartSpec {
	appConfig := config.GetConfig()
	return go_helm_client.ChartSpec{
		ReleaseName:     "longhorn",
		ChartName:       appConfig.Modules.Additional.Storage.Longhorn.ChartRef,
		Version:         appConfig.Modules.Additional.Storage.Longhorn.ChartVersion,
		Namespace:       appConfig.Modules.Additional.Storage.Longhorn.Namespace,
		CreateNamespace: true,
		Atomic:          true,
		Timeout:         time.Second * 600,
		ValuesYaml:      templates.GetHelmValuesByTemplate("templates/helm-apps/releases/additional-modules/longhorn.yml.tmpl"),
	}
}

func getSecretsStoreSpec() go_helm_client.ChartSpec {
	appConfig := config.GetConfig()
	return go_helm_client.ChartSpec{
		ReleaseName:     "csi-secrets-store",
		ChartName:       appConfig.Modules.Additional.Storage.SecretsStoreCsiDriver.ChartRef,
		Version:         appConfig.Modules.Additional.Storage.SecretsStoreCsiDriver.ChartVersion,
		Namespace:       appConfig.Modules.Additional.Storage.SecretsStoreCsiDriver.Namespace,
		CreateNamespace: true,
		Atomic:          true,
		Timeout:         time.Second * 60,
		ValuesYaml:      templates.GetHelmValuesByTemplate("templates/helm-apps/releases/additional-modules/secrets-store-csi-driver.yml.tmpl"),
	}
}

func ApplyCharts() {
	appConfig := config.GetConfig()
	helm_client.AddHelmRepos("kube-system", HELM_REPOS)
	if appConfig.Modules.Additional.Storage.Longhorn.Enabled {
		helm_client.InstallChart(getLonghornSpec())
	} else {
		helm_client.DeleteChart(getLonghornSpec())
	}
	if appConfig.Modules.Additional.Storage.SecretsStoreCsiDriver.Enabled {
		helm_client.InstallChart(getSecretsStoreSpec())
	} else {
		helm_client.DeleteChart(getSecretsStoreSpec())
	}
}
