package shellclient

import (
	"context"
	"path/filepath"
	"testing"
)

func TestCurrentControllerInputWithoutConfig(t *testing.T) {
	c := &Client{ConfigPath: filepath.Join(t.TempDir(), "mirage-ui.json")}
	got, hasStore, err := c.CurrentControllerInput()
	if err != nil {
		t.Fatalf("CurrentControllerInput() error = %v", err)
	}
	if hasStore {
		t.Fatal("CurrentControllerInput() unexpectedly reported stored config")
	}
	if got != defaultControllerInput {
		t.Fatalf("CurrentControllerInput() = %q, want %q", got, defaultControllerInput)
	}
}

func TestSaveAndResetControllerInput(t *testing.T) {
	c := &Client{ConfigPath: filepath.Join(t.TempDir(), "mirage-ui.json")}

	if err := c.SaveControllerInput("https://ctrl.example.com"); err != nil {
		t.Fatalf("SaveControllerInput() error = %v", err)
	}

	got, hasStore, err := c.CurrentControllerInput()
	if err != nil {
		t.Fatalf("CurrentControllerInput() error = %v", err)
	}
	if !hasStore {
		t.Fatal("CurrentControllerInput() did not report stored config")
	}
	if got != "https://ctrl.example.com" {
		t.Fatalf("CurrentControllerInput() = %q, want stored controller", got)
	}

	if err := c.ResetControllerInput(); err != nil {
		t.Fatalf("ResetControllerInput() error = %v", err)
	}
	got, hasStore, err = c.CurrentControllerInput()
	if err != nil {
		t.Fatalf("CurrentControllerInput() error = %v", err)
	}
	if hasStore {
		t.Fatal("CurrentControllerInput() still reported stored config after reset")
	}
	if got != defaultControllerInput {
		t.Fatalf("CurrentControllerInput() = %q, want default %q", got, defaultControllerInput)
	}
}

func TestToggleAutoStartup(t *testing.T) {
	fake := &fakeStartup{}
	c := &Client{
		ConfigPath: filepath.Join(t.TempDir(), "mirage-ui.json"),
		Startup:    fake,
	}

	enabled, err := c.GetAutoStartUp(context.Background())
	if err != nil {
		t.Fatalf("GetAutoStartUp() error = %v", err)
	}
	if enabled {
		t.Fatal("GetAutoStartUp() = true, want false")
	}

	if err := c.SetAutoStartUp(context.Background()); err != nil {
		t.Fatalf("SetAutoStartUp() error = %v", err)
	}
	enabled, err = c.GetAutoStartUp(context.Background())
	if err != nil {
		t.Fatalf("GetAutoStartUp() error = %v", err)
	}
	if !enabled {
		t.Fatal("GetAutoStartUp() = false, want true after toggle")
	}
}

func TestAuthKeyRoundTrip(t *testing.T) {
	c := &Client{ConfigPath: filepath.Join(t.TempDir(), "mirage-ui.json")}

	got, err := c.AuthKey()
	if err != nil {
		t.Fatalf("AuthKey() error = %v", err)
	}
	if got != "" {
		t.Fatalf("AuthKey() = %q, want empty string initially", got)
	}

	if err := c.SetAuthKey("tskey-auth-k123"); err != nil {
		t.Fatalf("SetAuthKey() error = %v", err)
	}
	got, err = c.AuthKey()
	if err != nil {
		t.Fatalf("AuthKey() error = %v", err)
	}
	if got != "tskey-auth-k123" {
		t.Fatalf("AuthKey() = %q, want persisted auth key", got)
	}

	if err := c.SetAuthKey(""); err != nil {
		t.Fatalf("SetAuthKey(clear) error = %v", err)
	}
	got, err = c.AuthKey()
	if err != nil {
		t.Fatalf("AuthKey() error = %v", err)
	}
	if got != "" {
		t.Fatalf("AuthKey() after clear = %q, want empty string", got)
	}
}

type fakeStartup struct {
	enabled bool
}

func (f *fakeStartup) Enabled() (bool, error) {
	return f.enabled, nil
}

func (f *fakeStartup) SetEnabled(v bool) error {
	f.enabled = v
	return nil
}
