//go:build e2e

package testkit

import (
	"testing"

	"github.com/tidbcloud/AgentTeam/e2e/client"
)

// Environment is a ready API target for scenario tests (embedded server or remote URL).
type Environment struct {
	Profile Profile
	BaseURL string
	API     *client.Client
}

// Harness is an alias kept for readability in older scenarios; prefer Environment.
type Harness = Environment

// StartEnvironment prepares the backend under test according to Config.
func StartEnvironment(t *testing.T) Environment {
	t.Helper()
	cfg := LoadConfig(t)

	switch cfg.Profile {
	case ProfileRemote:
		if cfg.BaseURL == "" {
			t.Fatalf("remote profile requires AGENTTEAM_E2E_BASE_URL or AGENTTEAM_E2E_ACCOUNTS_FILE.baseURL")
		}
		t.Logf("using remote agentteam environment: %s", cfg.BaseURL)
		return Environment{
			Profile: ProfileRemote,
			BaseURL: cfg.BaseURL,
			API:     client.New(cfg.BaseURL),
		}
	case ProfileEmbedded:
		local := StartLocalE2EServer(t)
		t.Logf("using embedded agentteam-e2e-server: %s", local.BaseURL)
		return Environment{
			Profile: ProfileEmbedded,
			BaseURL: local.BaseURL,
			API:     client.New(local.BaseURL),
		}
	default:
		t.Fatalf("unsupported e2e profile: %q", cfg.Profile)
		return Environment{}
	}
}

// StartHarness is equivalent to StartEnvironment.
func StartHarness(t *testing.T) Harness {
	t.Helper()
	return StartEnvironment(t)
}
