package suite

import (
	"os"
	"strconv"
	"strings"

	"github.com/sirupsen/logrus"
)

// Config contains the test configuration values
type Config struct {
	// BaseURL is the location of the Datahub cluster.
	// Configure using the `BASE_URL` env variable.
	BaseURL string
	// AdminEmail is a tenant admin email. For localdev this defaults to `lemon@example.com`
	// Configure using the `ADMIN_EMAIL` env variable.
	AdminEmail string
	// AdminPassword is the password for AdminEmail
	// Configure using the `ADMIN_PASSWORD` env variable.
	AdminPassword string
	// UserEmail can be any other user in the system. For localdev this defaults to `lemonjr@example.com`
	// Configure using the `USER_EMAIL` env variable.
	UserEmail string
	// UserPassword is the password for UserEmail
	// Configure using the `USER_PASSWORD` env variable.
	UserPassword string
	// TenantSlug is the slug for the organization the AdminEmail and UserEmail are members of. This
	// is used during login and in URL construction
	// Configure using the `BASE_URL` env variable.
	TenantSlug string
	// TenantName is the name of the organization the AdminEmail and UserEmail are members of.
	// Configure using the `TENANT_SLUG` env variable.
	TenantName string
	// IncludeTenantSlug indicates if the URL requires prefixing the tenant slug to the endpoint path.
	// In localdev, this is false, for other environments like CLOUD or DEV is should be set to true.
	// Configure using the `INCLUDE_TENANT_SLUG` env variable.
	IncludeTenantSlug bool
	// SkipCleanup indicates if the cleanup step to be skipped. This allows you to then look at and
	// debug a failing test run. This defaults to False, which means the test will attempt to cleanup
	// any objects it created
	// Configure using the `SKIP_CLEANUP` env variable.
	SkipCleanup bool
}

// Configuration returns the test suite configuration. The values are parsed from the env.
func (s *Suite) Configuration() Config {
	cfg := Config{
		BaseURL:           "http://localhost:9898",
		AdminEmail:        "lemon@example.com",
		AdminPassword:     "localdev",
		UserEmail:         "lemonjr@example.com",
		UserPassword:      "localdev",
		TenantSlug:        "lemonade-shop",
		TenantName:        "lemonade shop",
		IncludeTenantSlug: false,
		SkipCleanup:       false,
	}

	if u := os.Getenv("BASE_URL"); u != "" {
		cfg.BaseURL = strings.TrimSuffix(u, "/")
	}
	if email := os.Getenv("ADMIN_EMAIL"); email != "" {
		cfg.AdminEmail = strings.TrimSpace(email)
	}
	if password := os.Getenv("ADMIN_PASSWORD"); password != "" {
		cfg.AdminPassword = strings.TrimSpace(password)
	}

	if email := os.Getenv("USER_EMAIL"); email != "" {
		cfg.UserEmail = strings.TrimSpace(email)
	}
	if password := os.Getenv("USER_PASSWORD"); password != "" {
		cfg.UserPassword = strings.TrimSpace(password)
	}

	if tenant := os.Getenv("TENANT_NAME"); tenant != "" {
		cfg.TenantName = strings.TrimSpace(tenant)
	}
	if slug := os.Getenv("TENANT_SLUG"); slug != "" {
		cfg.TenantSlug = strings.TrimSpace(slug)
	}

	if includeTenantSlugStr := os.Getenv("INCLUDE_TENANT_SLUG"); includeTenantSlugStr != "" {
		b, err := strconv.ParseBool(includeTenantSlugStr)
		if err != nil {
			logrus.Warn(err)
		}
		cfg.IncludeTenantSlug = b
	}

	if skip := os.Getenv("SKIP_CLEANUP"); skip != "" {
		b, err := strconv.ParseBool(skip)
		if err != nil {
			logrus.Warn(err)
		}
		cfg.SkipCleanup = b
	}

	return cfg
}
