package config

import (
	"log"
	"os"
	"path/filepath"
	"testing"
)

func TestFindAbraConfig(t *testing.T) {
	wd, err := os.Getwd()
	if err != nil {
		log.Fatal(err)
	}
	tests := []struct {
		Dir    string
		Config string
	}{
		{
			Dir:    "testdata/abraconfig1",
			Config: filepath.Join(wd, "testdata/abraconfig1/abra.yaml"),
		},
		{
			Dir:    "testdata/abraconfig1/subdir",
			Config: filepath.Join(wd, "testdata/abraconfig1/abra.yaml"),
		},
		{
			Dir:    "testdata/abraconfig2",
			Config: filepath.Join(wd, "testdata/abraconfig2/abra.yml"),
		},
		{
			Dir:    "testdata/abraconfig2/subdir",
			Config: filepath.Join(wd, "testdata/abraconfig2/abra.yml"),
		},
		{
			Dir:    "testdata",
			Config: "",
		},
	}

	for _, tc := range tests {
		t.Run(tc.Dir, func(t *testing.T) {
			config := findAbraConfig(tc.Dir)
			if config != tc.Config {
				t.Errorf("\nwant: %s\ngot:  %s", tc.Config, config)
			}
		})
	}
}

func TestLoadAbraConfigGetAbraDir(t *testing.T) {
	wd, err := os.Getwd()
	if err != nil {
		log.Fatal(err)
	}
	t.Setenv("ABRA_DIR", "")

	t.Run("default", func(t *testing.T) {
		cfg := LoadAbraConfig()
		wantAbraDir := os.ExpandEnv("$HOME/.abra")
		if cfg.GetAbraDir() != wantAbraDir {
			t.Errorf("\nwant: %s\ngot:  %s", wantAbraDir, cfg.GetAbraDir())
		}
	})

	t.Run("from config file", func(t *testing.T) {
		t.Cleanup(func() { os.Chdir(wd) })
		err = os.Chdir(filepath.Join(wd, "testdata/abraconfig1"))
		if err != nil {
			log.Fatal(err)
		}

		cfg := LoadAbraConfig()
		wantAbraDir := filepath.Join(wd, "testdata/abraconfig1/foobar")
		if cfg.GetAbraDir() != wantAbraDir {
			t.Errorf("\nwant: %s\ngot:  %s", wantAbraDir, cfg.GetAbraDir())
		}
	})

	t.Run("default when config file is empty", func(t *testing.T) {
		t.Cleanup(func() { os.Chdir(wd) })
		err := os.Chdir(filepath.Join(wd, "testdata/abraconfig2"))
		if err != nil {
			log.Fatal(err)
		}

		cfg := LoadAbraConfig()
		wantAbraDir := os.ExpandEnv("$HOME/.abra")
		if cfg.GetAbraDir() != wantAbraDir {
			t.Errorf("\nwant: %s\ngot:  %s", wantAbraDir, cfg.GetAbraDir())
		}
	})

	t.Run("from env variable", func(t *testing.T) {
		t.Setenv("ABRA_DIR", "foo")
		cfg := LoadAbraConfig()
		wantAbraDir := "foo"
		if cfg.GetAbraDir() != wantAbraDir {
			t.Errorf("\nwant: %s\ngot:  %s", wantAbraDir, cfg.GetAbraDir())
		}
	})
}

func TestLoadAbraConfigServersDir(t *testing.T) {
	wd, err := os.Getwd()
	if err != nil {
		log.Fatal(err)
	}
	t.Setenv("ABRA_DIR", "")

	t.Run("default", func(t *testing.T) {
		cfg := LoadAbraConfig()
		wantServersDir := os.ExpandEnv("$HOME/.abra/servers")
		if cfg.GetServersDir() != wantServersDir {
			t.Errorf("\nwant: %s\ngot:  %s", wantServersDir, cfg.GetServersDir())
		}
	})

	t.Run("from config file", func(t *testing.T) {
		t.Cleanup(func() { os.Chdir(wd) })
		err = os.Chdir(filepath.Join(wd, "testdata/abraconfig1"))
		if err != nil {
			log.Fatal(err)
		}

		cfg := LoadAbraConfig()
		log.Println(cfg)
		wantServersDir := filepath.Join(wd, "testdata/abraconfig1/foobar/servers")
		if cfg.GetServersDir() != wantServersDir {
			t.Errorf("\nwant: %s\ngot:  %s", wantServersDir, cfg.GetServersDir())
		}
	})
}
