package test

import (
	"os"
	"path"

	appPkg "coopcloud.tech/abra/pkg/app"
	"coopcloud.tech/abra/pkg/envfile"
	"coopcloud.tech/abra/pkg/log"
	"coopcloud.tech/abra/pkg/recipe"
)

var (
	TestFolder    = os.ExpandEnv("$PWD/../../tests/resources/test_folder")
	ValidAbraConf = os.ExpandEnv("$PWD/../../tests/resources/valid_abra_config")
)

// make sure these are in alphabetical order
var (
	TFolders = []string{"folder1", "folder2"}
	TFiles   = []string{"bar.env", "foo.env"}
)

var (
	AppName    = "ecloud"
	ServerName = "evil.corp"
)

var ExpectedAppEnv = envfile.AppEnv{
	"DOMAIN":        "ecloud.evil.corp",
	"RECIPE":        "ecloud",
	"SMTP_AUTHTYPE": "login",
}

var ExpectedApp = appPkg.App{
	Name:   AppName,
	Recipe: recipe.Get(ExpectedAppEnv["RECIPE"]),
	Domain: ExpectedAppEnv["DOMAIN"],
	Env:    ExpectedAppEnv,
	Path:   ExpectedAppFile.Path,
	Server: ExpectedAppFile.Server,
}

var ExpectedAppFile = appPkg.AppFile{
	Path:   path.Join(ValidAbraConf, "servers", ServerName, AppName+".env"),
	Server: ServerName,
}

var ExpectedAppFiles = map[string]appPkg.AppFile{
	AppName: ExpectedAppFile,
}

// RmServerAppRecipe deletes the test server / app / recipe.
func RmServerAppRecipe() {
	testAppLink := os.ExpandEnv("$HOME/.abra/servers/foo.com")
	if err := os.Remove(testAppLink); err != nil {
		log.Fatal(err)
	}

	testRecipeLink := os.ExpandEnv("$HOME/.abra/recipes/test")
	if err := os.Remove(testRecipeLink); err != nil {
		log.Fatal(err)
	}
}

// MkServerAppRecipe symlinks the test server / app / recipe.
func MkServerAppRecipe() {
	RmServerAppRecipe()

	testAppDir := os.ExpandEnv("$PWD/../../tests/resources/testapp")
	testAppLink := os.ExpandEnv("$HOME/.abra/servers/foo.com")
	if err := os.Symlink(testAppDir, testAppLink); err != nil {
		log.Fatal(err)
	}

	testRecipeDir := os.ExpandEnv("$PWD/../../tests/resources/testrecipe")
	testRecipeLink := os.ExpandEnv("$HOME/.abra/recipes/test")
	if err := os.Symlink(testRecipeDir, testRecipeLink); err != nil {
		log.Fatal(err)
	}
}
