package cluster

import (
	"testing"
	"time"

	"github.com/argoproj/argo-cd/v3/test/e2e/fixture"
)

// Context implements the "given" part of given/when/then.
// It embeds fixture.TestState to provide test-specific state that enables parallel test execution.
type Context struct {
	*fixture.TestState

	project     string
	server      string
	upsert      bool
	namespaces  []string
	bearerToken string
}

func Given(t *testing.T) *Context {
	t.Helper()
	state := fixture.EnsureCleanState(t)
	return GivenWithSameState(state)
}

// GivenWithSameState creates a new Context that shares the same TestState as an existing context.
// Use this when you need multiple fixture contexts within the same test.
func GivenWithSameState(ctx fixture.TestContext) *Context {
	ctx.T().Helper()
	return &Context{TestState: fixture.NewTestStateFromContext(ctx), project: "default"}
}

func (c *Context) Name(name string) *Context {
	c.SetName(name)
	return c
}

func (c *Context) Server(server string) *Context {
	c.server = server
	return c
}

func (c *Context) Namespaces(namespaces []string) *Context {
	c.namespaces = namespaces
	return c
}

func (c *Context) And(block func()) *Context {
	block()
	return c
}

func (c *Context) When() *Actions {
	time.Sleep(fixture.WhenThenSleepInterval)
	return &Actions{context: c}
}

func (c *Context) Project(project string) *Context {
	c.project = project
	return c
}

func (c *Context) BearerToken(bearerToken string) *Context {
	c.bearerToken = bearerToken
	return c
}

func (c *Context) Upsert(upsert bool) *Context {
	c.upsert = upsert
	return c
}
