package utils

import (
	"context"
	"fmt"
	"time"

	"github.com/tidbcloud/account/commons/config"
	"github.com/tidbcloud/account/internel/dao/entity"
	"github.com/tidbcloud/tiinf/monitor/tilog"
)

func GetCentralBackendDns() string {
	return config.Global().ServerConfig.BackendDNS
}

func GetPocManualBackupReservedDays() int {
	return *config.Global().ServerConfig.PocManualBackupReservedDays
}

const (
	timeFormat = "2006-01-02 15:04"
)

// ConvertUtcTime returns UTC time based on UTC offset
// Example: 2021-06-03 16:19 UTC +08:00
func ConvertUtcTime(t time.Time, utcOffset int32) string {
	var offsetStr string
	switch {
	case utcOffset > 0:
		offsetStr = fmt.Sprintf("UTC +%02d:00", utcOffset)
	case utcOffset < 0:
		offsetStr = fmt.Sprintf("UTC -%02d:00", utcOffset)
	default:
		offsetStr = "UTC ±00:00"
	}

	return fmt.Sprintf("%s %s", t.UTC().Add(time.Duration(utcOffset)*time.Hour).Format(timeFormat), offsetStr)
}

func GetPocStatusUpdateRelatedManagerEmails(ctx context.Context, tenant *entity.Tenant) []string {
	logger := tilog.WithContextL(ctx)
	tos := []string{config.Global().ServerConfig.BizOpsTeamEmail}
	tenant.AccountManager = GetOrgAccountManager(ctx, tenant.ID.Uint64())
	if tenant.AccountManager != "" {
		tos = append(tos, tenant.AccountManager)
	}

	logger.Sugar().Infof("poc status update related emails: %s", tos)
	return tos
}
