package app_test

import (
	"encoding/json"
	"fmt"
	"reflect"
	"testing"

	appPkg "coopcloud.tech/abra/pkg/app"
	"coopcloud.tech/abra/pkg/config"
	"coopcloud.tech/abra/pkg/envfile"
	"coopcloud.tech/abra/pkg/recipe"
	testPkg "coopcloud.tech/abra/pkg/test"
	"github.com/docker/docker/api/types/filters"
	"github.com/google/go-cmp/cmp"
	"github.com/stretchr/testify/assert"
)

func TestNewApp(t *testing.T) {
	app, err := appPkg.NewApp(testPkg.ExpectedAppEnv, testPkg.AppName, testPkg.ExpectedAppFile)
	if err != nil {
		t.Fatal(err)
	}
	if !reflect.DeepEqual(app, testPkg.ExpectedApp) {
		t.Fatalf("did not get expected app type. Expected: %s; Got: %s", app, testPkg.ExpectedApp)
	}
}

func TestReadAppEnvFile(t *testing.T) {
	app, err := appPkg.ReadAppEnvFile(testPkg.ExpectedAppFile, testPkg.AppName)
	if err != nil {
		t.Fatal(err)
	}
	if !reflect.DeepEqual(app, testPkg.ExpectedApp) {
		t.Fatalf("did not get expected app type. Expected: %s; Got: %s", app, testPkg.ExpectedApp)
	}
}

func TestGetApp(t *testing.T) {
	app, err := appPkg.GetApp(testPkg.ExpectedAppFiles, testPkg.AppName)
	if err != nil {
		t.Fatal(err)
	}
	if !reflect.DeepEqual(app, testPkg.ExpectedApp) {
		t.Fatalf("did not get expected app type. Expected: %s; Got: %s", app, testPkg.ExpectedApp)
	}
}

func TestGetComposeFiles(t *testing.T) {
	r := recipe.Get("abra-test-recipe")
	err := r.EnsureExists()
	if err != nil {
		t.Fatal(err)
	}

	tests := []struct {
		appEnv       map[string]string
		composeFiles []string
	}{
		{
			map[string]string{},
			[]string{
				fmt.Sprintf("%s/compose.yml", r.Dir),
			},
		},
		{
			map[string]string{"COMPOSE_FILE": "compose.yml"},
			[]string{
				fmt.Sprintf("%s/compose.yml", r.Dir),
			},
		},
		{
			map[string]string{"COMPOSE_FILE": "compose.extra_secret.yml"},
			[]string{
				fmt.Sprintf("%s/compose.extra_secret.yml", r.Dir),
			},
		},
		{
			map[string]string{"COMPOSE_FILE": "compose.yml:compose.extra_secret.yml"},
			[]string{
				fmt.Sprintf("%s/compose.yml", r.Dir),
				fmt.Sprintf("%s/compose.extra_secret.yml", r.Dir),
			},
		},
	}

	for _, test := range tests {
		composeFiles, err := r.GetComposeFiles(test.appEnv)
		if err != nil {
			t.Fatal(err)
		}
		assert.Equal(t, composeFiles, test.composeFiles)
	}
}

func TestGetComposeFilesError(t *testing.T) {
	r := recipe.Get("abra-test-recipe")
	err := r.EnsureExists()
	if err != nil {
		t.Fatal(err)
	}

	tests := []struct{ appEnv map[string]string }{
		{map[string]string{"COMPOSE_FILE": "compose.yml::compose.foo.yml"}},
		{map[string]string{"COMPOSE_FILE": "doesnt.exist.yml"}},
	}

	for _, test := range tests {
		_, err := r.GetComposeFiles(test.appEnv)
		if err == nil {
			t.Fatalf("should have failed: %v", test.appEnv)
		}
	}
}

func TestFilters(t *testing.T) {
	oldDir := config.RECIPES_DIR
	config.RECIPES_DIR = "./testdata"
	defer func() {
		config.RECIPES_DIR = oldDir
	}()

	app, err := appPkg.NewApp(envfile.AppEnv{
		"DOMAIN": "test.example.com",
		"RECIPE": "test-recipe",
	}, "test_example_com", appPkg.AppFile{
		Path:   "./testdata/filtertest.end",
		Server: "local",
	})
	if err != nil {
		t.Fatal(err)
	}

	f, err := app.Filters(false, false)
	if err != nil {
		t.Error(err)
	}
	compareFilter(t, f, map[string]map[string]bool{
		"name": {
			"test_example_com": true,
		},
	})

	f2, err := app.Filters(false, true)
	if err != nil {
		t.Error(err)
	}
	compareFilter(t, f2, map[string]map[string]bool{
		"name": {
			"^test_example_com": true,
		},
	})

	f3, err := app.Filters(true, false)
	if err != nil {
		t.Error(err)
	}
	compareFilter(t, f3, map[string]map[string]bool{
		"name": {
			"test_example_com_bar": true,
			"test_example_com_foo": true,
		},
	})

	f4, err := app.Filters(true, true)
	if err != nil {
		t.Error(err)
	}
	compareFilter(t, f4, map[string]map[string]bool{
		"name": {
			"^test_example_com_bar": true,
			"^test_example_com_foo": true,
		},
	})

	f5, err := app.Filters(false, false, "foo")
	if err != nil {
		t.Error(err)
	}
	compareFilter(t, f5, map[string]map[string]bool{
		"name": {
			"test_example_com_foo": true,
		},
	})
}

func compareFilter(t *testing.T, f1 filters.Args, f2 map[string]map[string]bool) {
	t.Helper()
	j1, err := f1.MarshalJSON()
	if err != nil {
		t.Error(err)
	}
	j2, err := json.Marshal(f2)
	if err != nil {
		t.Error(err)
	}
	if diff := cmp.Diff(string(j2), string(j1)); diff != "" {
		t.Errorf("filters mismatch (-want +got):\n%s", diff)
	}
}

func TestWriteRecipeVersionOverwrite(t *testing.T) {
	app, err := appPkg.GetApp(testPkg.ExpectedAppFiles, testPkg.AppName)
	if err != nil {
		t.Fatal(err)
	}

	defer t.Cleanup(func() {
		if err := app.WipeRecipeVersion(); err != nil {
			t.Fatal(err)
		}
	})

	assert.Equal(t, "", app.Recipe.EnvVersion)

	if err := app.WriteRecipeVersion("foo", false); err != nil {
		t.Fatal(err)
	}

	app, err = appPkg.GetApp(testPkg.ExpectedAppFiles, testPkg.AppName)
	if err != nil {
		t.Fatal(err)
	}

	assert.Equal(t, "foo", app.Recipe.EnvVersion)

	app.Recipe.Dirty = true
	if err := app.WriteRecipeVersion("foo+U", false); err != nil {
		t.Fatal(err)
	}

	app, err = appPkg.GetApp(testPkg.ExpectedAppFiles, testPkg.AppName)
	if err != nil {
		t.Fatal(err)
	}

	assert.Equal(t, "foo+U", app.Recipe.EnvVersion)
}
