/*
Copyright 2021.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package controllers

import (
	"context"
	"crypto/md5"
	"encoding/hex"
	appsv1 "k8s.io/api/apps/v1"
	corev1 "k8s.io/api/core/v1"
	"k8s.io/apimachinery/pkg/api/errors"
	v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
	"strconv"
	"time"

	"k8s.io/apimachinery/pkg/runtime"
	ctrl "sigs.k8s.io/controller-runtime"
	"sigs.k8s.io/controller-runtime/pkg/client"
	"sigs.k8s.io/controller-runtime/pkg/log"

	stackv1 "github.com/ethereum-optimism/optimism/go/stackman/api/v1"
)

// DeployerReconciler reconciles a Deployer object
type DeployerReconciler struct {
	client.Client
	Scheme *runtime.Scheme
}

//+kubebuilder:rbac:groups=stack.optimism-stacks.net,resources=deployers,verbs=get;list;watch;create;update;patch;delete
//+kubebuilder:rbac:groups=stack.optimism-stacks.net,resources=deployers/status,verbs=get;update;patch
//+kubebuilder:rbac:groups=stack.optimism-stacks.net,resources=deployers/finalizers,verbs=update
//+kubebuilder:rbac:groups=apps,resources=deployments,verbs=get;list;watch;create;update;patch;delete
//+kubebuilder:rbac:groups=core,resources=services;pods;configmaps,verbs=get;list;watch;create;update;patch;delete

// Reconcile is part of the main kubernetes reconciliation loop which aims to
// move the current state of the cluster closer to the desired state.
// TODO(user): Modify the Reconcile function to compare the state specified by
// the Deployer object against the actual cluster state, and then
// perform operations to make the cluster state reflect the state specified by
// the user.
//
// For more details, check Reconcile and its Result here:
// - https://pkg.go.dev/sigs.k8s.io/controller-runtime@v0.10.0/pkg/reconcile
func (r *DeployerReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
	lgr := log.FromContext(ctx)

	crd := &stackv1.Deployer{}
	if err := r.Get(ctx, req.NamespacedName, crd); err != nil {
		if errors.IsNotFound(err) {
			lgr.Info("deployer resource not found, ignoring")
			return ctrl.Result{}, nil
		}

		lgr.Error(err, "error getting deployer")
		return ctrl.Result{}, err
	}

	created, err := GetOrCreateResource(ctx, r, func() client.Object {
		return r.entrypointsCfgMap(crd)
	}, ObjectNamespacedName(crd.ObjectMeta, "entrypoints"), &corev1.ConfigMap{})
	if err != nil {
		return ctrl.Result{}, err
	}
	if created {
		return ctrl.Result{Requeue: true}, nil
	}

	deployment := &appsv1.Deployment{}
	created, err = GetOrCreateResource(ctx, r, func() client.Object {
		return r.deployment(crd)
	}, ObjectNamespacedName(crd.ObjectMeta, "deployer"), deployment)
	if err != nil {
		return ctrl.Result{}, err
	}
	if created {
		return ctrl.Result{Requeue: true}, nil
	}

	argsHash := r.deploymentArgsHash(crd)
	if deployment.Labels["args_hash"] != argsHash {
		err := r.Update(ctx, r.deployment(crd))
		if err != nil {
			lgr.Error(err, "error updating deployer deployment")
			return ctrl.Result{}, err
		}

		return ctrl.Result{RequeueAfter: 30 * time.Second}, nil
	}

	created, err = GetOrCreateResource(ctx, r, func() client.Object {
		return r.service(crd)
	}, ObjectNamespacedName(crd.ObjectMeta, "deployer"), &corev1.Service{})
	if err != nil {
		return ctrl.Result{}, err
	}
	if created {
		return ctrl.Result{Requeue: true}, nil
	}

	return ctrl.Result{}, nil
}

// SetupWithManager sets up the controller with the Manager.
func (r *DeployerReconciler) SetupWithManager(mgr ctrl.Manager) error {
	return ctrl.NewControllerManagedBy(mgr).
		For(&stackv1.Deployer{}).
		Owns(&corev1.ConfigMap{}).
		Owns(&appsv1.Deployment{}).
		Owns(&corev1.Service{}).
		Complete(r)
}

func (r *DeployerReconciler) labels(crd *stackv1.Deployer) map[string]string {
	return map[string]string{
		"app":          "deployer",
		"deployer_crd": crd.Namespace,
	}
}

func (r *DeployerReconciler) entrypointsCfgMap(crd *stackv1.Deployer) *corev1.ConfigMap {
	cfgMap := &corev1.ConfigMap{
		ObjectMeta: ObjectMeta(crd.ObjectMeta, "entrypoints", r.labels(crd)),
		Data: map[string]string{
			"entrypoint.sh": DeployerEntrypoint,
		},
	}
	ctrl.SetControllerReference(crd, cfgMap, r.Scheme)
	return cfgMap
}

func (r *DeployerReconciler) deploymentArgsHash(crd *stackv1.Deployer) string {
	h := md5.New()
	h.Write([]byte(crd.Spec.Image))
	h.Write([]byte(crd.Spec.L1URL))
	h.Write([]byte(strconv.Itoa(crd.Spec.L1TimeoutSeconds)))
	for _, ev := range crd.Spec.Env {
		h.Write([]byte(ev.String()))
	}
	return hex.EncodeToString(h.Sum(nil))
}

func (r *DeployerReconciler) deployment(crd *stackv1.Deployer) *appsv1.Deployment {
	replicas := int32(1)
	defaultMode := int32(0o777)
	deployment := &appsv1.Deployment{
		ObjectMeta: ObjectMeta(crd.ObjectMeta, "deployer", map[string]string{
			"app":          "deployer",
			"deployer_crd": crd.Namespace,
			"args_hash":    r.deploymentArgsHash(crd),
		}),
		Spec: appsv1.DeploymentSpec{
			Replicas: &replicas,
			Selector: &v1.LabelSelector{
				MatchLabels: map[string]string{
					"app": "deployer",
				},
			},
			Template: corev1.PodTemplateSpec{
				ObjectMeta: v1.ObjectMeta{
					Labels: r.labels(crd),
				},
				Spec: corev1.PodSpec{
					RestartPolicy: corev1.RestartPolicyAlways,
					InitContainers: []corev1.Container{
						{
							Name:            "wait-for-l1",
							Image:           "mslipper/wait-for-it:latest",
							ImagePullPolicy: corev1.PullAlways,
							Args: []string{
								Hostify(crd.Spec.L1URL),
								"-t",
								strconv.Itoa(crd.Spec.L1TimeoutSeconds),
							},
						},
					},
					Containers: []corev1.Container{
						{
							Name:            "deployer",
							Image:           crd.Spec.Image,
							ImagePullPolicy: corev1.PullAlways,
							Command: []string{
								"/bin/bash",
								"/opt/entrypoints/entrypoint.sh",
							},
							Env: append([]corev1.EnvVar{
								{
									Name:  "L1_NODE_WEB3_URL",
									Value: crd.Spec.L1URL,
								},
								{
									Name:  "NO_COMPILE",
									Value: "1",
								},
								{
									Name:  "AUTOMATICALLY_TRANSFER_OWNERSHIP",
									Value: "true",
								},
							}, crd.Spec.Env...),
							VolumeMounts: []corev1.VolumeMount{
								{
									Name:      "entrypoints",
									MountPath: "/opt/entrypoints",
								},
							},
							Ports: []corev1.ContainerPort{
								{
									ContainerPort: 8081,
								},
							},
						},
					},
					Volumes: []corev1.Volume{
						{
							Name: "entrypoints",
							VolumeSource: corev1.VolumeSource{
								ConfigMap: &corev1.ConfigMapVolumeSource{
									LocalObjectReference: corev1.LocalObjectReference{
										Name: ObjectName(crd.ObjectMeta, "entrypoints"),
									},
									DefaultMode: &defaultMode,
								},
							},
						},
					},
				},
			},
		},
	}
	ctrl.SetControllerReference(crd, deployment, r.Scheme)
	return deployment
}

func (r *DeployerReconciler) service(crd *stackv1.Deployer) *corev1.Service {
	service := &corev1.Service{
		ObjectMeta: ObjectMeta(crd.ObjectMeta, "deployer", r.labels(crd)),
		Spec: corev1.ServiceSpec{
			Selector: map[string]string{
				"app": "deployer",
			},
			Ports: []corev1.ServicePort{
				{
					Name: "web",
					Port: 8081,
				},
			},
		},
	}
	ctrl.SetControllerReference(crd, service, r.Scheme)
	return service
}

const DeployerEntrypoint = `
#!/bin/bash
set -e
cd /optimism/packages/contracts
yarn run deploy

function envSet() {
    VAR=$1
    export $VAR=$(cat ./dist/dumps/addresses.json | jq -r ".$2")
}

# set the address to the proxy gateway if possible
envSet L1_STANDARD_BRIDGE_ADDRESS Proxy__OVM_L1StandardBridge
if [ $L1_STANDARD_BRIDGE_ADDRESS == null ]; then
    envSet L1_STANDARD_BRIDGE_ADDRESS L1StandardBridge
fi

envSet L1_CROSS_DOMAIN_MESSENGER_ADDRESS Proxy__OVM_L1CrossDomainMessenger
if [ $L1_CROSS_DOMAIN_MESSENGER_ADDRESS == null ]; then
    envSet L1_CROSS_DOMAIN_MESSENGER_ADDRESS L1CrossDomainMessenger
fi

# build the dump file
yarn run build:dump

echo "Starting server."

# service the addresses and dumps
python3 -m http.server \
    --bind "0.0.0.0" 8081 \
    --directory ./dist/dumps
`
