package defaults

import (
	"time"

	"github.com/qsfera/server/pkg/shared"
	"github.com/qsfera/server/pkg/structs"
	"github.com/qsfera/server/services/gateway/pkg/config"
)

// FullDefaultConfig returns a fully initialized default configuration
func FullDefaultConfig() *config.Config {
	cfg := DefaultConfig()
	EnsureDefaults(cfg)
	Sanitize(cfg)
	return cfg
}

// DefaultConfig returns a basic default configuration
func DefaultConfig() *config.Config {
	return &config.Config{
		Debug: config.Debug{
			Addr:   "127.0.0.1:9143",
			Token:  "",
			Pprof:  false,
			Zpages: false,
		},
		GRPC: config.GRPCConfig{
			Addr:      "127.0.0.1:9142",
			Namespace: "qsfera.api",
			Protocol:  "tcp",
		},
		Service: config.Service{
			Name: "gateway",
		},
		Reva:                       shared.DefaultRevaConfig(),
		CommitShareToStorageGrant:  true,
		ShareFolder:                "Shares",
		DisableHomeCreationOnLogin: true,
		TransferExpires:            24 * 60 * 60,
		Cache: config.Cache{
			ProviderCacheStore:      "noop",
			ProviderCacheNodes:      []string{"127.0.0.1:9233"},
			ProviderCacheDatabase:   "cache-providers",
			ProviderCacheTTL:        300 * time.Second,
			CreateHomeCacheStore:    "memory",
			CreateHomeCacheNodes:    []string{"127.0.0.1:9233"},
			CreateHomeCacheDatabase: "cache-createhome",
			CreateHomeCacheTTL:      300 * time.Second,
		},

		FrontendPublicURL: "https://localhost:9200",

		AppRegistryEndpoint:       "qsfera.api.app-registry",
		AuthAppEndpoint:           "qsfera.api.auth-app",
		AuthBasicEndpoint:         "qsfera.api.auth-basic",
		AuthMachineEndpoint:       "qsfera.api.auth-machine",
		AuthServiceEndpoint:       "qsfera.api.auth-service",
		GroupsEndpoint:            "qsfera.api.groups",
		PermissionsEndpoint:       "qsfera.api.settings",
		SharingEndpoint:           "qsfera.api.sharing",
		StoragePublicLinkEndpoint: "qsfera.api.storage-publiclink",
		StorageSharesEndpoint:     "qsfera.api.storage-shares",
		StorageUsersEndpoint:      "qsfera.api.storage-users",
		UsersEndpoint:             "qsfera.api.users",
		OCMEndpoint:               "qsfera.api.ocm",

		StorageRegistry: config.StorageRegistry{
			Driver: "spaces",
			JSON:   "",
		},
	}
}

// EnsureDefaults adds default values to the configuration if they are not set yet
func EnsureDefaults(cfg *config.Config) {
	if cfg.LogLevel == "" {
		cfg.LogLevel = "error"
	}

	if cfg.Reva == nil && cfg.Commons != nil {
		cfg.Reva = structs.CopyOrZeroValue(cfg.Commons.Reva)
	}

	if cfg.TokenManager == nil && cfg.Commons != nil && cfg.Commons.TokenManager != nil {
		cfg.TokenManager = &config.TokenManager{
			JWTSecret: cfg.Commons.TokenManager.JWTSecret,
		}
	} else if cfg.TokenManager == nil {
		cfg.TokenManager = &config.TokenManager{}
	}

	if cfg.TransferSecret == "" && cfg.Commons != nil && cfg.Commons.TransferSecret != "" {
		cfg.TransferSecret = cfg.Commons.TransferSecret
	}

	if cfg.GRPC.TLS == nil && cfg.Commons != nil {
		cfg.GRPC.TLS = structs.CopyOrZeroValue(cfg.Commons.GRPCServiceTLS)
	}
}

// Sanitize sanitized the configuration
func Sanitize(cfg *config.Config) {
	// nothing to sanitize here atm
}
