package common

import (
	"encoding/json"
	"errors"
	"fmt"
	"github.com/fsnotify/fsnotify"
	"github.com/spf13/viper"
	"os"
	"sec-cycle-scan/config"
)

func InitConfig() error {
	AppEnv = os.Getenv("APP_ENV")

	RootPath, _ = os.Getwd()

	in := "conf"
	if AppEnv != "" {
		in += "." + AppEnv
	} else { //默认未配置为dev环境
		in += "." + "dev"
	}

	v := viper.New()
	v.SetConfigName(in)
	v.SetConfigType("yaml")
	v.AddConfigPath(RootPath)

	err := v.ReadInConfig()
	if err != nil {
		return errors.New(fmt.Sprintf("Fatal error config file: %s \n", err))
	}
	v.WatchConfig()

	v.OnConfigChange(func(e fsnotify.Event) {
		err := v.Unmarshal(&CONFIG)
		if err != nil {
			fmt.Println(err)
		} else {
			fmt.Println("configuration file hot-loading success")
		}
	})

	if err = v.Unmarshal(&CONFIG); err != nil {
		return errors.New(fmt.Sprintf("config unmarshal error: %s \n", err))
	}

	//if AppEnv != "" {
	//	InitK8sSecretForPath(&CONFIG)
	//}

	VP = v
	fmt.Println("Configuration initialization completed")
	return nil
}

func InitK8sSecretForPath(s *config.Server) {
	paths := []string{"/etc/redis/conf", "/etc/system/conf", "/etc/db/conf", "/etc/bucket/conf"}
	for _, path := range paths {
		if err := mountConfig(s, path); err != nil {
			panic(fmt.Sprintf("k8s configuration initialization failed. path:%s err:%s", path, err))
		}
	}
}

func mountConfig(conf *config.Server, confFile string) error {
	data, err := os.ReadFile(confFile)
	if err != nil {
		return err
	}

	err = json.Unmarshal(data, conf)
	if err != nil {
		return err
	}

	return nil
}

//func InitK8sSecret(c *config.Agent) {
//
//	cfg, err := rest.InClusterConfig()
//	if err != nil {
//		panic(err)
//	}
//
//	client, err := kubernetes.NewForConfig(cfg)
//	if err != nil {
//		panic(err)
//	}
//
//	// 获取 Secret
//	secret, err := client.CoreV1().Secrets("default").Get(context.Background(), "sec-secret", v1.GetOptions{})
//	if err != nil {
//		panic(err)
//	}
//
//	redisPass, ok := secret.Data["redis_pass"]
//	if !ok {
//		panic("redis_pass not found in secret")
//	}
//	c.Redis.Password = string(redisPass)
//	c.AsynqRedis.Password = string(redisPass)
//
//	snykToken, ok := secret.Data["snyk_token"]
//	if !ok {
//		panic("snykPass not found in secret")
//	}
//	c.Snyk.SnykToken = string(snykToken)
//
//	authToken, ok := secret.Data["auth_token"]
//	if !ok {
//		panic("authToken not found in secret")
//	}
//	c.System.AuthToken = string(authToken)
//
//	s3AK, ok := secret.Data["s3_access_key"]
//	if !ok {
//		panic("s3_access_key not found in secret")
//	}
//	c.S3.AccessKey = string(s3AK)
//
//	s3SK, ok := secret.Data["s3_secret_key"]
//	if !ok {
//		panic("s3_secret_key not found in secret")
//	}
//	c.S3.SecretKey = string(s3SK)
//
//	s3Endpoint, ok := secret.Data["s3_endpoint"]
//	if !ok {
//		panic("s3_endpoint not found in secret")
//	}
//	c.S3.Endpoint = string(s3Endpoint)
//
//	s3Region, ok := secret.Data["s3_region"]
//	if !ok {
//		panic("s3Region not found in secret")
//	}
//	c.S3.Region = string(s3Region)
//
//	s3Bucket, ok := secret.Data["s3_Bucket"]
//	if !ok {
//		panic("s3Bucket not found in secret")
//	}
//	c.S3.Bucket = string(s3Bucket)
//
//}
