package helm

import (
	"testing"

	"freeleaps.com/gitops/initializer/api/v1alpha1"
	cmmeta "github.com/cert-manager/cert-manager/pkg/apis/meta/v1"
	corev1 "k8s.io/api/core/v1"
	networkingv1 "k8s.io/api/networking/v1"
	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

var pathType = networkingv1.PathTypeImplementationSpecific

var testCase = v1alpha1.ProjectInitialize{
	ObjectMeta: metav1.ObjectMeta{
		Name: "magicleaps-alpha",
	},
	Spec: v1alpha1.ProjectInitializeSpec{
		Repository: &v1alpha1.RepositorySpec{
			Plugin: "azure-devops",
			PluginConfig: map[string]string{
				"pat":             "3E9J1v0si6HRvyc1RSRgT791W9ZvMi4kBmrznfcIXB8mq6Trj9VkJQQJ99BBACAAAAArj0WRAAASAZDO32n4",
				"organizationUrl": "https://dev.azure.com/freeleaps",
			},
			RegisterToArgo: true,
			Project:        "magicleaps",
			Name:           "magicleaps",
			Type:           v1alpha1.Monorepo,
			Branch:         "develop",
		},
		Helm: &v1alpha1.HelmSpec{
			Generate: true,
			Metadata: v1alpha1.HelmMetadataSpec{
				Name:        "magicleaps",
				Description: "A Helm Chart of magicleaps, powered by Freeleaps.",
			},
			Global: v1alpha1.HelmGlobalValuesSpec{
				Registry:   "docker.io",
				Repository: "sunzhenyucn",
			},
			Components: []v1alpha1.HelmComponentSpec{
				{
					Name: "frontend",
					Ports: []corev1.ContainerPort{
						{
							Name:          "http",
							ContainerPort: 8080,
							Protocol:      corev1.ProtocolTCP,
						},
					},
					Replicas: 1,
					Resources: v1alpha1.HelmComponentResourceRequirementsSpec{
						Limits: v1alpha1.HelmComponentResourceSettingSpec{
							CPU:    "100m",
							Memory: "256Mi",
						},
						Requests: v1alpha1.HelmComponentResourceSettingSpec{
							CPU:    "50m",
							Memory: "128Mi",
						},
					},
					Image: v1alpha1.HelmComponentImageSpec{
						Repository:      "sunzhenyucn",
						Name:            "magicleaps-frontend",
						Tag:             "1.0.0",
						ImagePullPolicy: corev1.PullIfNotPresent,
					},
					Probes: v1alpha1.HelmComponentProbesSpec{
						Liveness: v1alpha1.HelmComponentProbeSpec{
							Type: v1alpha1.HTTPGet,
							Config: v1alpha1.HelmComponentProbeConfigSpec{
								Path: "/",
								Port: 8080,
							},
						},
						Readiness: v1alpha1.HelmComponentProbeSpec{
							Type: v1alpha1.HTTPGet,
							Config: v1alpha1.HelmComponentProbeConfigSpec{
								Path: "/",
								Port: 8080,
							},
						},
					},
					Services: []v1alpha1.HelmComponentServiceSpec{
						{
							Name:       "magicleaps-frontend-service",
							Type:       corev1.ServiceTypeClusterIP,
							Port:       80,
							TargetPort: 8080,
						},
					},
					Ingresses: []v1alpha1.HelmComponentIngressSpec{
						{
							Name:  "magicleaps-frontend-ingress",
							Class: "nginx",
							Host:  "alpha.magicleaps.mathmast.com",
							Rules: []networkingv1.HTTPIngressPath{
								{
									Path:     "/*",
									PathType: &pathType,
									Backend: networkingv1.IngressBackend{
										Service: &networkingv1.IngressServiceBackend{
											Name: "magicleaps-frontend-service",
											Port: networkingv1.ServiceBackendPort{
												Number: 80,
											},
										},
									},
								},
							},
							TLS: v1alpha1.HelmComponentIngressTLSSpec{
								Exists: false,
								Name:   "magicleaps-alpha-frontend-ingress-tls",
								IssuerRef: &cmmeta.ObjectReference{
									Name:  "mathmast-dot-com",
									Kind:  "ClusterIssuer",
									Group: "cert-manager.io",
								},
							},
						},
					},
					Configs: []v1alpha1.HelmComponentConfigSpec{
						{
							Name: "magicleaps-frontend-config",
							Type: v1alpha1.EnvironmentSecret,
							Data: []corev1.EnvVar{
								{
									Name:  "TZ",
									Value: "America/Settle",
								},
							},
						},
					},
				},

				{
					Name: "backend",
					Ports: []corev1.ContainerPort{
						{
							Name:          "http",
							ContainerPort: 8081,
							Protocol:      corev1.ProtocolTCP,
						},
					},
					Replicas: 1,
					Resources: v1alpha1.HelmComponentResourceRequirementsSpec{
						Limits: v1alpha1.HelmComponentResourceSettingSpec{
							CPU:    "200m",
							Memory: "512Mi",
						},
						Requests: v1alpha1.HelmComponentResourceSettingSpec{
							CPU:    "100m",
							Memory: "256Mi",
						},
					},
					Image: v1alpha1.HelmComponentImageSpec{
						Repository:      "sunzhenyucn",
						Name:            "magicleaps-backend",
						Tag:             "1.0.0",
						ImagePullPolicy: corev1.PullIfNotPresent,
					},
					Probes: v1alpha1.HelmComponentProbesSpec{
						Liveness: v1alpha1.HelmComponentProbeSpec{
							Type: v1alpha1.HTTPGet,
							Config: v1alpha1.HelmComponentProbeConfigSpec{
								Path: "/api/_/probe/liveness",
								Port: 8081,
							},
						},
						Readiness: v1alpha1.HelmComponentProbeSpec{
							Type: v1alpha1.HTTPGet,
							Config: v1alpha1.HelmComponentProbeConfigSpec{
								Path: "/api/_/probe/readiness",
								Port: 8081,
							},
						},
					},
					Services: []v1alpha1.HelmComponentServiceSpec{
						{
							Name:       "magicleaps-backend-service",
							Type:       corev1.ServiceTypeClusterIP,
							Port:       8081,
							TargetPort: 8081,
						},
					},
					Ingresses: []v1alpha1.HelmComponentIngressSpec{},
					Configs: []v1alpha1.HelmComponentConfigSpec{
						{
							Name: "magicleaps-backend-config",
							Type: v1alpha1.EnvironmentSecret,
							Data: []corev1.EnvVar{
								{
									Name:  "TZ",
									Value: "America/Settle",
								},
								{
									Name:  "MONGODB_HOST",
									Value: "localhost",
								},
								{
									Name:  "MONGODB_PORT",
									Value: "27017",
								},
								{
									Name:  "MONGODB_NAME",
									Value: "interview",
								},
								{
									Name:  "EMAIL_USER",
									Value: "your@freeleaps.com",
								},
								{
									Name:  "EMAIL_PASSWORD",
									Value: "your-password",
								},
								{
									Name:  "SUPER_ADMIN",
									Value: "your@email.com",
								},
								{
									Name:  "TWILIO_ACCOUNT_SID",
									Value: "",
								},
								{
									Name:  "TWILIO_AUTH_TOKEN",
									Value: "",
								},
								{
									Name:  "EVELUATION_TASK_FOLDER_BASE",
									Value: "temp/interview/eveluation_task/",
								},
								{
									Name:  "LOG_DIR",
									Value: "logs",
								},
								{
									Name:  "APP_LOG_FILE",
									Value: "app.log",
								},
								{
									Name:  "APP_LOG_LEVEL",
									Value: "INFO",
								},
							},
						},
					},
				},
			},
		},
	},
}

func TestGenerate(t *testing.T) {
	gen := &HelmGenerator{
		Instance: &testCase,
	}
	if err := gen.Generate(); err != nil {
		t.Error(err)
	}
}
