package cmii_operator

import (
	"fmt"
	"testing"
	"time"
	"wdd.io/agent-go/assert"
	"wdd.io/agent-go/utils"
)

var CmiiDevNamespaceList = []string{
	"uavcloud-dev",
	"uavcloud-devflight",
	"uavcloud-devoperation",
}

func TestFindAppNotHealthyOrRestartCountGreaterThanN(t *testing.T) {

	deploymentRestartCountGreaterThanN := FindAppNotHealthyOrRestartCountGreaterThanN("devflight", 10)
	deploymentRestartCountGreaterThanN = FilterAllCmiiAppSoft(deploymentRestartCountGreaterThanN)

	for _, deploymentInterface := range deploymentRestartCountGreaterThanN {
		println()
		utils.BeautifulPrint(deploymentInterface)
		println()
	}

}

func TestFindDeploymentReplicasSmallerThanN(t *testing.T) {

	deploymentReplicasSmallerThanN := FindDeploymentReplicasSmallerThanN("devflight", 0)

	for _, deploymentInterface := range deploymentReplicasSmallerThanN {
		println()
		utils.BeautifulPrint(deploymentInterface)
		println()
	}

}

func TestFindCmiiMiddlewarePodInterface(t *testing.T) {
	middlewarePodInterface := FindCmiiMiddlewarePodInterface(integration)

	for _, middlePod := range middlewarePodInterface {
		println()
		utils.BeautifulPrint(middlePod)
		println()
	}
}

func TestBackupAllDeploymentFromEnv(t *testing.T) {

	BackupAllDeploymentFromEnv(devFlight)

}

func TestBackupAllCmiiDeploymentToMap(t *testing.T) {
	backendMap, frontendMap := BackupAllCmiiDeploymentToMap("demo")

	utils.BeautifulPrint(backendMap)
	utils.BeautifulPrint(frontendMap)

}

func TestRollBackCmiiDeploymentFromUpdateLog(t *testing.T) {
	updateLog := RollBackCmiiDeploymentFromUpdateLog("2024-01-10-14-37-07 uavcloud-devflight       cmii-uav-depotautoreturn                12345678            123sdsa45678")

	assert.Equal(t, updateLog, true, "roll back from update log failed !")
}

func TestRestartCmiiBackendDeployment(t *testing.T) {

	RestartCmiiBackendDeployment("dev")
}

func TestRestartCmiiFrontendDeployment(t *testing.T) {
	RestartCmiiFrontendDeployment("dev")
}

func TestRestartCmiiDeployment(t *testing.T) {
	cmiiEnv := integration
	appName := "cmii-uav-cloud-live"

	kill := CmiiOperator.DeploymentRestartByKill(cmiiEnv, appName)
	assert.Equal(t, kill, true, "have unhealthy pod !")

	check := CmiiOperator.DeploymentStatusCheck(cmiiEnv, appName, 180)
	assert.Equal(t, check, true, "DeploymentStatusCheck failed !")
}

func TestFindDeploymentNotHealthy(t *testing.T) {

	for _, devNamespace := range CmiiDevNamespaceList {
		notHealthy := FindDeploymentNotHealthy(devNamespace)
		notHealthy = FilterAllCmiiAppSoft(notHealthy)
		for _, deploymentInterface := range notHealthy {
			utils.BeautifulPrint(deploymentInterface)
		}
	}
}

func TestFindAllNodeNotHealthy(t *testing.T) {
	start := time.Now()
	allNodeNotHealthy := FindAllNodeNotHealthy()
	elapsed := time.Since(start).Milliseconds()
	fmt.Printf("执行耗时： %d ms\n", elapsed)

	allNodeNotHealthy = FilterAllCmiiNodeSoft(allNodeNotHealthy)

	assert.Equal(t, len(allNodeNotHealthy), 0, "have unhealthy pod !")

	for _, nodeInterface := range allNodeNotHealthy {
		println()
		utils.BeautifulPrint(nodeInterface)
		println()
	}

}

func TestFindPodNotHealthy(t *testing.T) {

	podNotHealthy := FindPodNotHealthy("devfl")
	podNotHealthy = FilterAllCmiiPodSoft(podNotHealthy)

	for _, podInterface := range podNotHealthy {
		t.Logf("[%s] [%s]", podInterface.Name, podInterface.PodPhase)
	}
}

func TestFindPodNotHealthy_And_Delete(t *testing.T) {
	podNotHealthy := FindPodNotHealthy("devf")
	podNotHealthy = FilterAllCmiiPodSoft(podNotHealthy)

	for _, podInterface := range podNotHealthy {
		t.Logf("[%s] [%s]", podInterface.Name, podInterface.PodPhase)
		podDelete := CmiiOperator.PodDelete(podInterface.Namespace, podInterface.Name)

		assert.Equal(t, podDelete, true, "delete of ", podInterface.Namespace, podInterface.Name, " failed !")
	}

}

func TestFilterAllCmiiAppStrict(t *testing.T) {
	allInterface := CmiiOperator.DeploymentAllInterface("devflight")
	FilterAllCmiiAppStrict(allInterface)
}

func TestRestartDeploymentFromList(t *testing.T) {
	allInterface := CmiiOperator.DeploymentAllInterface("devflight")
	allInterface = FilterAllCmiiAppSoft(allInterface)

	RestartDeploymentFromList(allInterface)

}

func TestUpdateCmiiImageTagFromNameTagMap(t *testing.T) {

	cmii530BackendMap := map[string]string{
		"cmii-admin-data":               "5.3.0",
		"cmii-admin-gateway":            "5.3.0",
		"cmii-admin-user":               "5.3.0",
		"cmii-open-gateway":             "5.3.0",
		"cmii-suav-supervision":         "5.3.0",
		"cmii-uav-airspace":             "5.3.0",
		"cmii-uav-alarm":                "5.3.0",
		"cmii-uav-brain":                "5.3.0",
		"cmii-uav-cloud-live":           "5.3.0",
		"cmii-uav-cms":                  "5.3.0",
		"cmii-uav-data-post-process":    "5.3.0",
		"cmii-uav-developer":            "5.3.0",
		"cmii-uav-device":               "5.3.0",
		"cmii-uav-emergency":            "5.3.0",
		"cmii-uav-gateway":              "5.3.0",
		"cmii-uav-gis-server":           "5.3.0",
		"cmii-uav-industrial-portfolio": "5.3.0",
		"cmii-uav-integration":          "5.3.0",
		"cmii-uav-logger":               "5.3.0",
		"cmii-uav-material-warehouse":   "5.3.0",
		"cmii-uav-mission":              "5.3.0",
		"cmii-uav-mqtthandler":          "5.3.0",
		"cmii-uav-notice":               "5.3.0",
		"cmii-uav-oauth":                "5.3.0",
		"cmii-uav-process":              "5.3.0",
		"cmii-uav-surveillance":         "5.3.0",
		"cmii-uav-threedsimulation":     "5.3.0",
		"cmii-uav-tower":                "5.3.0",
		"cmii-uav-user":                 "5.3.0",
		"cmii-uav-waypoint":             "5.3.0",
		//"cmii-uav-grid-datasource":      "5.2.0-24810",
		//"cmii-uav-grid-engine":          "5.1.0",
		//"cmii-uav-grid-manage":          "5.1.0",
	}

	cmii530FrontendMap := map[string]string{
		"cmii-suav-platform-supervision":     "5.3.0",
		"cmii-suav-platform-supervisionh5":   "5.3.0",
		"cmii-uav-platform":                  "5.3.0",
		"cmii-uav-platform-ai-brain":         "5.3.0",
		"cmii-uav-platform-armypeople":       "5.3.0",
		"cmii-uav-platform-base":             "5.3.0",
		"cmii-uav-platform-cms-portal":       "5.3.0",
		"cmii-uav-platform-detection":        "5.3.0",
		"cmii-uav-platform-emergency-rescue": "5.3.0",
		"cmii-uav-platform-logistics":        "5.3.0",
		"cmii-uav-platform-media":            "5.3.0",
		"cmii-uav-platform-multiterminal":    "5.3.0",
		"cmii-uav-platform-mws":              "5.3.0",
		"cmii-uav-platform-oms":              "5.3.0",
		"cmii-uav-platform-open":             "5.3.0",
		"cmii-uav-platform-securityh5":       "5.3.0",
		"cmii-uav-platform-seniclive":        "5.3.0",
		"cmii-uav-platform-share":            "5.3.0",
		"cmii-uav-platform-splice":           "5.3.0",
		"cmii-uav-platform-threedsimulation": "5.3.0",
		"cmii-uav-platform-visualization":    "5.3.0",
		//"cmii-uav-platform-security":         "4.1.6",
	}

	result := UpdateCmiiImageTagFromNameTagMap("demo", cmii530BackendMap)
	utils.BeautifulPrint(result)

	result = UpdateCmiiImageTagFromNameTagMap("demo", cmii530FrontendMap)
	utils.BeautifulPrint(result)

}

func TestUpdateCmiiDeploymentImageTag(t *testing.T) {

	cmiiEnv := "test"
	appName := "cmii-suav-supervision"
	newTag := "5.2.0-011901"

	tag := UpdateCmiiDeploymentImageTag(cmiiEnv, appName, newTag)
	assert.Equal(t, tag, true, "update image tag failed !")
	utils.SplitLinePrint()

	check := CmiiOperator.DeploymentStatusCheck(cmiiEnv, appName, 180)
	assert.Equal(t, check, true, "deployment run failed!")

}
