package c_middle

import (
	"encoding/base64"
	"github.com/go-playground/validator/v10"
	"wdd.io/agent-common/logger"
	"wdd.io/agent-common/utils"
	"wdd.io/agent-deploy/z_dep"
)

var (
	log                = logger.Log
	DefaultMysqlConfig = &MySQlConfig{
		MySQLNodePort:           "33306",
		MySQLRootPasswordBase64: base64.StdEncoding.EncodeToString([]byte("QzfXQhd3bQ")),
		MySQLRootPassword:       "QzfXQhd3bQ",
		MySQLK8sAdminPassword:   "fP#UaH6qQ3)8",
	}

	DefaultRabbitConfig = &RabbitMQConfig{
		RabbitNodePort:          "35672",
		RabbitDashboardNodePort: "35675",
		RabbitPassword:          "nYcRN91r._hj",
		RabbitPasswordBase64:    "blljUk45MXIuX2hq",
	}

	DefaultEmqxConfig = &EmqxConfig{
		EmqxNodePort:          "31883",
		EmqxDashboardNodePort: "38085",
		EmqxWebSocketNodePort: "38083",
		EmqxPassword:          "odD8#Ve7.B",
	}

	DefaultMongoConfig = &MongoConfig{
		MongoPassword: "REdPza8#oVlt",
	}
)

type MySQlConfig struct {
	z_dep.CommonEnvironmentConfig
	MySQLNodePort           string
	MySQLRootPassword       string `validate:"required" comment:"string"`
	MySQLRootPasswordBase64 string `validate:"required" comment:"base64"`
	MySQLK8sAdminPassword   string `validate:"required" comment:"string"`
}

type RedisConfig struct {
	z_dep.CommonEnvironmentConfig
}

type RabbitMQConfig struct {
	z_dep.CommonEnvironmentConfig
	RabbitNodePort          string
	RabbitDashboardNodePort string
	RabbitPassword          string
	RabbitPasswordBase64    string
}

type NacosConfig struct {
	z_dep.CommonEnvironmentConfig
	MySQLK8sAdminPassword string
	NacosNodePort         string
}

type MongoConfig struct {
	z_dep.CommonEnvironmentConfig
	MongoPassword string
	MongoNodePort string
}

type EmqxConfig struct {
	z_dep.CommonEnvironmentConfig
	EmqxNodePort          string
	EmqxDashboardNodePort string
	EmqxWebSocketNodePort string
	EmqxPassword          string
}

func (emqx *EmqxConfig) MidEmqxDeploy(commonEnv *z_dep.CommonEnvironmentConfig) bool {

	// copy
	utils.CopySameFields(commonEnv, emqx)

	validate := validator.New()
	err := validate.Struct(emqx)
	if err != nil {
		log.ErrorF("backend config validate error: %v\n", err)
		return false
	}

	if !z_dep.ParseEnvToApplyFile(emqx, CmiiEmqxTemplate, z_dep.EmqxApplyFilePath) {
		return false
	}

	return true
}

func MidEmqxDeploy(commonEnv *z_dep.CommonEnvironmentConfig) bool {
	return commonEnv.ParseCommonEnvToApplyFile(CmiiEmqxTemplate, z_dep.EmqxApplyFilePath)
}

func (mongo *MongoConfig) MidMongoDeploy(commonEnv *z_dep.CommonEnvironmentConfig) bool {

	// copy
	utils.CopySameFields(commonEnv, mongo)

	validate := validator.New()
	err := validate.Struct(mongo)
	if err != nil {
		log.ErrorF("backend config validate error: %v\n", err)
		return false
	}

	if !z_dep.ParseEnvToApplyFile(mongo, CmiiMongoTemplate, z_dep.MongoApplyFilePath) {
		return false
	}

	return true
}
func MidMongoDeploy(commonEnv *z_dep.CommonEnvironmentConfig) bool {
	return commonEnv.ParseCommonEnvToApplyFile(CmiiMongoTemplate, z_dep.MongoApplyFilePath)
}

func (rabbit *RabbitMQConfig) MidRabbitMQDeploy(commonEnv *z_dep.CommonEnvironmentConfig) bool {

	// copy
	utils.CopySameFields(commonEnv, rabbit)

	validate := validator.New()
	err := validate.Struct(rabbit)
	if err != nil {
		log.ErrorF("backend config validate error: %v\n", err)
		return false
	}

	if !z_dep.ParseEnvToApplyFile(rabbit, CmiiRabbitMQTemplate, z_dep.RabbitMQApplyFilePath) {
		return false
	}

	return true
}
func MidRabbitMQDeploy(commonEnv *z_dep.CommonEnvironmentConfig) bool {
	return commonEnv.ParseCommonEnvToApplyFile(CmiiRabbitMQTemplate, z_dep.RabbitMQApplyFilePath)
}

func (redis *RedisConfig) MidRedisDeploy(commonEnv *z_dep.CommonEnvironmentConfig) bool {

	// copy
	utils.CopySameFields(commonEnv, redis)

	validate := validator.New()
	err := validate.Struct(redis)
	if err != nil {
		log.ErrorF("backend config validate error: %v\n", err)
		return false
	}

	if !z_dep.ParseEnvToApplyFile(redis, CmiiRedisTemplate, z_dep.RedisApplyFilePath) {
		return false
	}

	return true
}
func MidRedisDeploy(commonEnv *z_dep.CommonEnvironmentConfig) bool {
	return commonEnv.ParseCommonEnvToApplyFile(CmiiRedisTemplate, z_dep.RedisApplyFilePath)
}

func (mysql *MySQlConfig) MidMySQlDeploy(commonEnv *z_dep.CommonEnvironmentConfig) bool {
	// copy
	utils.CopySameFields(commonEnv, mysql)

	validate := validator.New()
	err := validate.Struct(mysql)
	if err != nil {
		log.ErrorF("backend config validate error: %v\n", err)
		return false
	}

	if !z_dep.ParseEnvToApplyFile(mysql, CmiiMySQLTemplate, z_dep.MySQLApplyFilePath) {
		return false
	}

	return true
}

func MidMySQlDeploy(commonEnv *z_dep.CommonEnvironmentConfig) bool {
	return commonEnv.ParseCommonEnvToApplyFile(CmiiMySQLTemplate, z_dep.MySQLApplyFilePath)
}

func (nacos *NacosConfig) MidNacosDeploy(commonEnv *z_dep.CommonEnvironmentConfig) bool {
	// copy
	utils.CopySameFields(commonEnv, nacos)

	validate := validator.New()
	err := validate.Struct(nacos)
	if err != nil {
		log.ErrorF("backend config validate error: %v\n", err)
		return false
	}

	if !z_dep.ParseEnvToApplyFile(nacos, CmiiNacosTemplate, z_dep.NacosApplyFilePath) {
		return false
	}

	return true
}

func MidNacosDeploy(commonEnv *z_dep.CommonEnvironmentConfig) bool {
	return commonEnv.ParseCommonEnvToApplyFile(CmiiNacosTemplate, z_dep.NacosApplyFilePath)
}

func PVCDeploy(commonEnv *z_dep.CommonEnvironmentConfig) bool {
	return commonEnv.ParseCommonEnvToApplyFile(CmiiPVCTemplate, z_dep.PVCApplyFilePath)
}
