package certpaths

import (
	"os"
	"path/filepath"
	"strings"
	"testing"

	"git.cloudinit.dev/coreci/orca/internal/paths"
)

const defaultHomeSubdir = ".orca"

func TestPaths_HonorORCAHOME(t *testing.T) {
	dir := t.TempDir()
	t.Setenv("ORCA_HOME", dir)
	t.Setenv("ORCA_DB", "")

	cases := []struct {
		name string
		got  string
		file string
	}{
		{"CACertPath", CACertPath(), "ca.crt"},
		{"CAKeyPath", CAKeyPath(), "ca.key"},
		{"ServerCertPath", ServerCertPath(), "server.crt"},
		{"ServerKeyPath", ServerKeyPath(), "server.key"},
		{"SSHKeyPath", SSHKeyPath(), "orca_ssh_key"},
		{"SSHPubPath", SSHPubPath(), "orca_ssh_key.pub"},
		{"KnownHostsPath", KnownHostsPath(), "known_hosts"},
	}
	for _, tc := range cases {
		t.Run(tc.name, func(t *testing.T) {
			want := filepath.Join(dir, tc.file)
			if tc.got != want {
				t.Errorf("%s = %q, want %q", tc.name, tc.got, want)
			}
		})
	}

	if got, want := DBPath(), filepath.Join(dir, "orca.db"); got != want {
		t.Errorf("DBPath = %q, want %q", got, want)
	}

	if got, want := Dir(), dir; got != want {
		t.Errorf("Dir = %q, want %q", got, want)
	}
}

func TestShim_DelegatesDirToPaths(t *testing.T) {
	dir := t.TempDir()
	t.Setenv("ORCA_HOME", dir)
	if got, want := Dir(), paths.Root(); got != want {
		t.Errorf("Dir() = %q, paths.Root() = %q (shim must delegate)", got, want)
	}
	if got, want := Dir(), dir; got != want {
		t.Errorf("Dir() = %q, want %q", got, want)
	}
}

func TestDBPath_OrcaDBOverride(t *testing.T) {
	home := t.TempDir()
	t.Setenv("ORCA_HOME", home)
	custom := filepath.Join(t.TempDir(), "custom.db")
	t.Setenv("ORCA_DB", custom)

	if got := DBPath(); got != custom {
		t.Errorf("DBPath = %q, want %q (ORCA_DB override)", got, custom)
	}
}

func TestDBPath_OrcaDBEmptyStringFallsBackToHome(t *testing.T) {
	home := t.TempDir()
	t.Setenv("ORCA_HOME", home)
	t.Setenv("ORCA_DB", "")

	want := filepath.Join(home, "orca.db")
	if got := DBPath(); got != want {
		t.Errorf("DBPath = %q, want %q", got, want)
	}
}

func TestDir_DefaultHomeFallback(t *testing.T) {
	os.Unsetenv("ORCA_HOME")
	os.Unsetenv("ORCA_DB")

	home, err := os.UserHomeDir()
	if err != nil {
		t.Skipf("os.UserHomeDir: %v (cannot verify default fallback)", err)
	}
	want := filepath.Join(home, defaultHomeSubdir)
	if got := Dir(); got != want {
		t.Errorf("Dir() default = %q, want %q", got, want)
	}
	if got := CACertPath(); got != filepath.Join(want, "ca.crt") {
		t.Errorf("CACertPath default = %q, want %q", got, filepath.Join(want, "ca.crt"))
	}
}

func TestDir_ORCAHOMEEmptyFallsBack(t *testing.T) {
	t.Setenv("ORCA_HOME", "")
	home, err := os.UserHomeDir()
	if err != nil {
		t.Skipf("os.UserHomeDir: %v", err)
	}
	want := filepath.Join(home, defaultHomeSubdir)
	if got := Dir(); got != want {
		t.Errorf("Dir() with empty ORCA_HOME = %q, want %q", got, want)
	}
}

func TestDir_ORCAHOMERelativePath(t *testing.T) {
	t.Setenv("ORCA_HOME", "relative/orca/home")
	if got, want := Dir(), "relative/orca/home"; got != want {
		t.Errorf("Dir() relative = %q, want %q", got, want)
	}
	if got, want := CACertPath(), filepath.Join("relative/orca/home", "ca.crt"); got != want {
		t.Errorf("CACertPath relative = %q, want %q", got, want)
	}
}

func TestAllPaths_AreConsistentWithDir(t *testing.T) {
	dir := t.TempDir()
	t.Setenv("ORCA_HOME", dir)
	t.Setenv("ORCA_DB", "")

	base := Dir()
	for _, p := range []string{
		CACertPath(), CAKeyPath(),
		ServerCertPath(), ServerKeyPath(),
		SSHKeyPath(), SSHPubPath(),
		KnownHostsPath(), DBPath(),
	} {
		if !strings.HasPrefix(p, base+string(filepath.Separator)) && p != filepath.Join(base, filepath.Base(p)) {
			t.Errorf("path %q is not under Dir() %q", p, base)
		}
	}
}

func TestShim_ReturnsV08FlatPaths(t *testing.T) {
	dir := t.TempDir()
	t.Setenv("ORCA_HOME", dir)
	t.Setenv("ORCA_DB", "")

	root := paths.Root()
	if got, want := CACertPath(), filepath.Join(root, "ca.crt"); got != want {
		t.Errorf("CACertPath = %q, want v0.8 flat %q", got, want)
	}
	if got, want := CAKeyPath(), filepath.Join(root, "ca.key"); got != want {
		t.Errorf("CAKeyPath = %q, want v0.8 flat %q", got, want)
	}
	if got, want := ServerCertPath(), filepath.Join(root, "server.crt"); got != want {
		t.Errorf("ServerCertPath = %q, want v0.8 flat %q", got, want)
	}
	if got, want := ServerKeyPath(), filepath.Join(root, "server.key"); got != want {
		t.Errorf("ServerKeyPath = %q, want v0.8 flat %q", got, want)
	}
	if got, want := SSHKeyPath(), filepath.Join(root, "orca_ssh_key"); got != want {
		t.Errorf("SSHKeyPath = %q, want v0.8 flat %q", got, want)
	}
	if got, want := SSHPubPath(), filepath.Join(root, "orca_ssh_key.pub"); got != want {
		t.Errorf("SSHPubPath = %q, want v0.8 flat %q", got, want)
	}
	if got, want := KnownHostsPath(), filepath.Join(root, "known_hosts"); got != want {
		t.Errorf("KnownHostsPath = %q, want v0.8 flat %q", got, want)
	}
	if got, want := DBPath(), filepath.Join(root, "orca.db"); got != want {
		t.Errorf("DBPath = %q, want v0.8 flat %q", got, want)
	}
}

func TestSSHPaths_Filenames(t *testing.T) {
	dir := t.TempDir()
	t.Setenv("ORCA_HOME", dir)
	if got, want := filepath.Base(SSHKeyPath()), "orca_ssh_key"; got != want {
		t.Errorf("SSHKeyPath base = %q, want %q", got, want)
	}
	if got, want := filepath.Base(SSHPubPath()), "orca_ssh_key.pub"; got != want {
		t.Errorf("SSHPubPath base = %q, want %q", got, want)
	}
	if got, want := filepath.Base(KnownHostsPath()), "known_hosts"; got != want {
		t.Errorf("KnownHostsPath base = %q, want %q", got, want)
	}
}
