package tconfig

import (
	"context"
	"os"
	"strings"
	"testing"

	"github.com/alcionai/clues"
	"github.com/stretchr/testify/require"

	"github.com/alcionai/corso/src/pkg/logger"
)

// M365TenantID returns a tenantID string representing the azureTenantID described
// by either the env var AZURE_TENANT_ID, the corso_test.toml config
// file or the default value (in that order of priority).  The default is a
// last-attempt fallback that will only work on alcion's testing org.
func M365TenantID(t *testing.T) string {
	cfg, err := ReadTestConfig()
	require.NoError(t, err, "retrieving m365 tenant ID from test configuration", clues.ToCore(err))

	return strings.ToLower(cfg[TestCfgAzureTenantID])
}

// M365TenantID returns a tenantID string representing the azureTenantID described
// by either the env var AZURE_TENANT_ID, the corso_test.toml config
// file or the default value (in that order of priority).  The default is a
// last-attempt fallback that will only work on alcion's testing org.
func GetM365TenantID(ctx context.Context) string {
	cfg, err := ReadTestConfig()
	if err != nil {
		logger.Ctx(ctx).Error(err, "retrieving m365 tenant ID from test configuration")
	}

	return strings.ToLower(cfg[TestCfgAzureTenantID])
}

// M365UserID returns an userID string representing the m365UserID described
// by either the env var CORSO_M365_TEST_USER_ID, the corso_test.toml config
// file or the default value (in that order of priority).  The default is a
// last-attempt fallback that will only work on alcion's testing org.
func M365UserID(t *testing.T) string {
	cfg, err := ReadTestConfig()
	require.NoError(t, err, "retrieving m365 user id from test configuration", clues.ToCore(err))

	return strings.ToLower(cfg[TestCfgUserID])
}

// GetM365UserID returns an userID string representing the m365UserID described
// by either the env var CORSO_M365_TEST_USER_ID, the corso_test.toml config
// file or the default value (in that order of priority).  The default is a
// last-attempt fallback that will only work on alcion's testing org.
func GetM365UserID(ctx context.Context) string {
	cfg, err := ReadTestConfig()
	if err != nil {
		logger.Ctx(ctx).Error(err, "retrieving m365 user id from test configuration")
	}

	return strings.ToLower(cfg[TestCfgUserID])
}

// SecondaryM365UserID returns an userID string representing the m365UserID
// described by either the env var CORSO_SECONDARY_M365_TEST_USER_ID, the
// corso_test.toml config file or the default value (in that order of priority).
// The default is a last-attempt fallback that will only work on alcion's
// testing org.
func SecondaryM365UserID(t *testing.T) string {
	cfg, err := ReadTestConfig()
	require.NoError(t, err, "retrieving secondary m365 user id from test configuration", clues.ToCore(err))

	return strings.ToLower(cfg[TestCfgSecondaryUserID])
}

// TertiaryM365UserID returns an userID string representing the m365UserID
// described by either the env var CORSO_TERTIARY_M365_TEST_USER_ID, the
// corso_test.toml config file or the default value (in that order of priority).
// The default is a last-attempt fallback that will only work on alcion's
// testing org.
func TertiaryM365UserID(t *testing.T) string {
	cfg, err := ReadTestConfig()
	require.NoError(t, err, "retrieving tertiary m365 user id from test configuration", clues.ToCore(err))

	return strings.ToLower(cfg[TestCfgTertiaryUserID])
}

// LoadTestM365SiteID returns a siteID string representing the m365SiteID
// described by either the env var CORSO_M365_LOAD_TEST_SITE_ID, the
// corso_test.toml config file or the default value (in that order of priority).
// The default is a last-attempt fallback that will only work on alcion's
// testing org.
func LoadTestM365SiteID(t *testing.T) string {
	cfg, err := ReadTestConfig()
	require.NoError(t, err, "retrieving load test m365 site id from test configuration", clues.ToCore(err))

	return strings.ToLower(cfg[TestCfgSiteID])
}

// LoadTestM365UserID returns an userID string representing the m365UserID
// described by either the env var CORSO_M365_LOAD_TEST_USER_ID, the
// corso_test.toml config file or the default value (in that order of priority).
// The default is a last-attempt fallback that will only work on alcion's
// testing org.
func LoadTestM365UserID(t *testing.T) string {
	cfg, err := ReadTestConfig()
	require.NoError(t, err, "retrieving load test m365 user id from test configuration", clues.ToCore(err))

	return strings.ToLower(cfg[TestCfgLoadTestUserID])
}

// expects cfg value to be a string representing an array such as:
// ["site1\,uuid","site2\,uuid"]
// the delimiter must be a |.
func LoadTestM365OrgSites(t *testing.T) []string {
	cfg, err := ReadTestConfig()
	require.NoError(t, err, "retrieving load test m365 org sites from test configuration %+v", clues.ToCore(err))

	// TODO: proper handling of site slice input.
	// sites := cfg[TestCfgLoadTestOrgSites]
	// sites = strings.TrimPrefix(sites, "[")
	// sites = strings.TrimSuffix(sites, "]")
	// sites = strings.ReplaceAll(sites, `"`, "")
	// sites = strings.ReplaceAll(sites, `'`, "")
	// sites = strings.ReplaceAll(sites, "|", ",")

	// return strings.Split(sites, ",")

	return []string{strings.ToLower(cfg[TestCfgSiteID])}
}

// expects cfg value to be a string representing an array such as:
// ["foo@example.com","bar@example.com"]
// the delimiter may be either a , or |.
func LoadTestM365OrgUsers(t *testing.T) []string {
	cfg, err := ReadTestConfig()
	require.NoError(t, err, "retrieving load test m365 org users from test configuration %+v", clues.ToCore(err))

	users := cfg[TestCfgLoadTestOrgUsers]
	users = strings.TrimPrefix(users, "[")
	users = strings.TrimSuffix(users, "]")
	users = strings.ReplaceAll(users, `"`, "")
	users = strings.ReplaceAll(users, `'`, "")
	users = strings.ReplaceAll(users, "|", ",")

	// a hack to skip using certain users when those accounts are
	// temporarily being co-opted for non-testing purposes.
	sl := strings.Split(users, ",")
	remove := os.Getenv("IGNORE_LOAD_TEST_USER_ID")

	if len(remove) == 0 {
		return sl
	}

	idx := -1

	for i, s := range sl {
		if s == remove {
			idx = i
			break
		}
	}

	return append(sl[:idx], sl[idx+1:]...)
}

// M365SiteID returns a siteID string representing the m365SiteID described
// by either the env var CORSO_M365_TEST_SITE_ID, the corso_test.toml config
// file or the default value (in that order of priority).  The default is a
// last-attempt fallback that will only work on alcion's testing org.
func M365SiteID(t *testing.T) string {
	cfg, err := ReadTestConfig()
	require.NoError(t, err, "retrieving m365 site id from test configuration: %+v", clues.ToCore(err))

	return strings.ToLower(cfg[TestCfgSiteID])
}

// M365SiteURL returns a site webURL string representing the m365SiteURL described
// by either the env var CORSO_M365_TEST_SITE_URL, the corso_test.toml config
// file or the default value (in that order of priority).  The default is a
// last-attempt fallback that will only work on alcion's testing org.
func M365SiteURL(t *testing.T) string {
	cfg, err := ReadTestConfig()
	require.NoError(t, err, "retrieving m365 site url from test configuration: %+v", clues.ToCore(err))

	return strings.ToLower(cfg[TestCfgSiteURL])
}

// GetM365SiteID returns a siteID string representing the m365SiteID described
// by either the env var CORSO_M365_TEST_SITE_ID, the corso_test.toml config
// file or the default value (in that order of priority).  The default is a
// last-attempt fallback that will only work on alcion's testing org.
func GetM365SiteID(ctx context.Context) string {
	cfg, err := ReadTestConfig()
	if err != nil {
		logger.Ctx(ctx).Error(err, "retrieving m365 site id from test configuration")
	}

	return strings.ToLower(cfg[TestCfgSiteID])
}

// SecondaryM365SiteID returns a siteID string representing the secondarym365SiteID described
// by either the env var CORSO_SECONDARY_M365_TEST_SITE_ID, the corso_test.toml config
// file or the default value (in that order of priority).  The default is a
// last-attempt fallback that will only work on alcion's testing org.
func SecondaryM365SiteID(t *testing.T) string {
	cfg, err := ReadTestConfig()
	require.NoError(t, err, "retrieving secondary m365 site id from test configuration: %+v", clues.ToCore(err))

	return strings.ToLower(cfg[TestCfgSecondarySiteID])
}

// GetM365TeamID returns a groupID string representing the m365TeamID described
// by either the env var CORSO_M365_TEST_TEAM_ID, the corso_test.toml config
// file or the default value (in that order of priority).  The default is a
// last-attempt fallback that will only work on alcion's testing org.
func GetM365TeamID(ctx context.Context) string {
	cfg, err := ReadTestConfig()
	if err != nil {
		logger.Ctx(ctx).Error(err, "retrieving m365 team id from test configuration")
	}

	return strings.ToLower(cfg[TestCfgTeamID])
}

// GetM365TeamSiteID returns a site ID string representing the m365TeamSiteID
// described by either the env var CORSO_M365_TEST_TEAM_SITE_ID, the
// corso_test.toml config file or the default value (in that order of priority).
// The default is a last-attempt fallback that will only work on alcion's
// testing org.
func GetM365TeamSiteID(ctx context.Context) string {
	cfg, err := ReadTestConfig()
	if err != nil {
		logger.Ctx(ctx).Error(err, "retrieving m365 team id from test configuration")
	}

	return strings.ToLower(cfg[TestCfgTeamSiteID])
}

// UnlicensedM365UserID returns an userID string representing the m365UserID
// described by either the env var CORSO_M365_TEST_UNLICENSED_USER, the
// corso_test.toml config file or the default value (in that order of priority).
// The default is a last-attempt fallback that will only work on alcion's
// testing org.
func UnlicensedM365UserID(t *testing.T) string {
	cfg, err := ReadTestConfig()
	require.NoError(t, err, "retrieving unlicensed m365 user id from test configuration: %+v", clues.ToCore(err))

	return strings.ToLower(cfg[TestCfgUnlicensedUserID])
}

// Teams

// M365TeamID returns a teamID string representing the m365TeamsID described
// by either the env var CORSO_M365_TEST_TEAM_ID, the corso_test.toml config
// file or the default value (in that order of priority).  The default is a
// last-attempt fallback that will only work on alcion's testing org.
func M365TeamID(t *testing.T) string {
	cfg, err := ReadTestConfig()
	require.NoError(t, err, "retrieving m365 team id from test configuration: %+v", clues.ToCore(err))

	return strings.ToLower(cfg[TestCfgTeamID])
}

// M365TeamEmail returns a teamEmail string representing the m365TeamsEmail described
// by either the env var CORSO_M365_TEST_TEAM_EMAIL, the corso_test.toml config
// file or the default value (in that order of priority) and should belong the same group as the one
// represented by CORSO_M365_TEST_TEAM_ID. The default is a
// last-attempt fallback that will only work on alcion's testing org.
func M365TeamEmail(t *testing.T) string {
	cfg, err := ReadTestConfig()
	require.NoError(t, err, "retrieving m365 team email from test configuration: %+v", clues.ToCore(err))

	return strings.ToLower(cfg[TestCfgTeamEmail])
}

// SecondaryM365TeamID returns a teamID string representing the secondarym365TeamID described
// by either the env var CORSO_SECONDARY_M365_TEST_TEAM_ID, the corso_test.toml config
// file or the default value (in that order of priority).  The default is a
// last-attempt fallback that will only work on alcion's testing org.
func SecondaryM365TeamID(t *testing.T) string {
	cfg, err := ReadTestConfig()
	require.NoError(t, err, "retrieving secondary m365 team id from test configuration: %+v", clues.ToCore(err))

	return strings.ToLower(cfg[TestCfgSecondaryTeamID])
}

// Groups

// M365GroupID returns a groupID string representing the m365GroupID described
// by either the env var CORSO_M365_TEST_GROUP_ID, the corso_test.toml config
// file or the default value (in that order of priority).  The default is a
// last-attempt fallback that will only work on alcion's testing org.
// NOTE: This Group will not have a Team associated with it.
func M365GroupID(t *testing.T) string {
	cfg, err := ReadTestConfig()
	require.NoError(t, err, "retrieving m365 group id from test configuration: %+v", clues.ToCore(err))

	return strings.ToLower(cfg[TestCfgGroupID])
}
