# Makefile for pOST Social Media Cluster
# Use npm/build commands in WSL but podman commands in windows powershell

CLUSTER_NAME = mern-cluster # namespace from "kubectl config get-contexts"
NAMESPACE = mern-distributed

SERVER_IMAGE = localhost/mern-server:latest
CLIENT_IMAGE = localhost/mern-client:latest
MANIFESTS = k8s-manifests.yaml

.PHONY: all up build load deploy clean clean-pods status help local public context serveronly start-cluster stop-cluster resetsecret ingress-test build-no-cache restart-cluster hey-npm hey-public hey-local start-dev pipeline-create-cluster

all: up

help:
	@echo "Usage:"
	@echo ""
	@echo "build:"
	@echo "  make up      - combined: Build images, load images, and deploy"
	@echo "  make build   - Only build the images with podman"
	@echo "  make build-no-cache - Only build the images with podman, without cache"
	@echo "  make load    - Load images into the kind cluster"
	@echo "  make deploy  - Apply k8s manifests to the cluster"
	@echo ""
	@echo "show:"
	@echo "  make local   - Expose application locally (localhost:3000)"
	@echo "  make public  - Expose application publicly (0.0.0.0:8080)"
	@echo ""
	@echo "kind:"
	@echo "  make pipeline-create-cluster - create new cluster"
	@echo "  make start-cluster   		- start the kind cluster again"
	@echo "  make restart-cluster  	- delete and create new kind cluster"
	@echo ""
	@echo "  make clean  			- Delete the cluster and remove tar files"
	@echo "  make clean-pods 		- Delete the pods, to restart them"
	@echo "  make status  			- Show status of pods and services"
	@echo ""
	@echo "helpers:"
	@echo "  make context 			- Change Namespace (-n mern-distributed)"
	@echo "  make serveronly 		- up server (build, save, load, deploy)"
	@echo ""
	@echo "  make hey-npm 			- load test, or: make hey-public/local"
	@echo ""
	@echo "  make start-dev		- for local development"


up: build start-cluster load deploy
	@echo "-------------------------------------------------------"
	@echo "Cluster setup Complete!"
	@echo "To access the application, run one of these in a separate terminal:"
	@echo "  make local  -> http://localhost:3000"
	@echo "  make public -> http://localhost:8080"
	@echo "-------------------------------------------------------"

local:
	@echo "open: http://localhost:3000"
	@echo "prefer the public version!"
	kubectl port-forward service/mern-client 3000:80 -n mern-distributed
# 	vorher: kubectl port-forward svc/mern-client 8080:80

public:
	@echo "open: http://localhost:8080"
	kubectl port-forward --address 0.0.0.0 service/mern-client 8080:80 -n mern-distributed
# 	vorher: kubectl port-forward --address 0.0.0.0 svc/mern-client 8080:80 -n mern-distributed

build:
	@echo "remove old image tars"
# 	@if [ -f mern-server.tar ]; then\
# 		rm mern-server.tar\
# 	fi
	rm -f mern-server.tar
	rm -f mern-client.tar
	@echo "Building server image..."

	podman build -t $(SERVER_IMAGE) ./server
# 	vorher: podman build -t mern-server:latest -f Dockerfile ./server

	@echo "Building client image..."
	podman build -t $(CLIENT_IMAGE) ./client

	@echo "Saving images to tar for kind..."
	podman save -o mern-server.tar $(SERVER_IMAGE)
# 	vorher: podman save mern-server:latest -o mern-server.tar
	podman save -o mern-client.tar $(CLIENT_IMAGE)

build-no-cache:
	@echo "remove old image tars"
	rm mern-server.tar
	rm mern-client.tar

	@echo "Building server image..."
	podman build --no-cache -t $(SERVER_IMAGE) ./server
# 	vorher: podman build -t mern-server:latest -f Dockerfile ./server
	@echo "Building client image..."
	podman build --no-cache -t $(CLIENT_IMAGE) ./client

	@echo "Saving images to tar for kind..."
	podman save -o mern-server.tar $(SERVER_IMAGE)
# 	vorher: podman save mern-server:latest -o mern-server.tar
	podman save -o mern-client.tar $(CLIENT_IMAGE)



start-cluster:
# 	create cluster if needed, (does not work in powershell)
# 	@echo "no clusters created (no checks)"; \
	
	@echo "start kind container"
	podman start mern-cluster-control-plane
# 	kind pod should automatically start:
# 	@if ! podman ps --all | grep "mern-cluster-control-plane"; then \
# 		echo "trying to start mern-cluster-control-plane container"; \
# 		podman start mern-cluster-control-plane
	@echo "start kind cluster"
	@if ! kind get clusters | grep $(CLUSTER_NAME); then \
		echo "Creating kind cluster $(CLUSTER_NAME)..."; \
		kind create cluster --name $(CLUSTER_NAME); \
	else \
		echo "Cluster $(CLUSTER_NAME) already exists. Restart with: make restart-cluster"; \
	fi

pipeline-create-cluster:
	@echo "create kind cluster"
	kind create cluster --name $(CLUSTER_NAME); \

restart-cluster:
	@echo "Deleting and Creating new kind cluster."
	echo "Deleting kind cluster $(CLUSTER_NAME)..."; \
	kind delete cluster --name $(CLUSTER_NAME); \
	echo "Creating kind cluster $(CLUSTER_NAME)..."; \
	kind create cluster --name $(CLUSTER_NAME); \
	make load
	@echo "finished restart"\

stop-cluster:
# 	fails and does SIGKILL the container
	@echo "stop kind container with cluster"
	podman stop mern-cluster-control-plane


load:
	@echo "Loading images into cluster..."
# 	kind load image-archive mern-server.tar --name mern-cluster
	kind load image-archive mern-server.tar --name $(CLUSTER_NAME)
	kind load image-archive mern-client.tar --name $(CLUSTER_NAME)

deploy:
	@echo "Applying manifests..."
	kubectl apply -f $(MANIFESTS)
	@echo "Waiting for deployments to be ready..."
# 	https://kubernetes.io/docs/reference/kubectl/generated/kubectl_rollout/

# 	kubectl config use-context kind-$(CLUSTER_NAME)
	kubectl rollout status deployment/mern-server -n $(NAMESPACE)
	kubectl rollout status deployment/mern-client -n $(NAMESPACE)

status:
	@echo "\n"
	kind get clusters
	@echo "\n"
	kubectl get svc -A
	@echo "\n"
	kubectl get pods,svc,pvc -n $(NAMESPACE)
	@echo "\n"
	kubectl get pods -n mern-distributed
	@echo "\n"
	@echo "show logs with: kubectl logs <mern-server-pod-name> -n mern-distributed"

clean:
	@echo "Deleting kind cluster..."
	kind delete cluster --name $(CLUSTER_NAME)
# 	@echo "Removing tar files..."
# 	rm -f mern-server.tar mern-client.tar

clean-pods:
	@echo "delete pods, will 'reactivate' pods in kind 'automatically'"
	kubectl delete pods --all -n mern-distributed
	
context:
	@echo "changing context..."
# 	kubectl config set-context --current --namespace=mern-distributed
	kubectl config set-context --current --namespace=$(NAMESPACE)

resetsecret: 
	@echo "reset webtoken secret"
	kubectl create secret generic mern-secrets \
	--from-literal=jwt-secret=$(openssl rand -base64 64) \
	-n $(NAMESPACE)

serveronly:
	@echo "Building server image..."
	podman build -t $(SERVER_IMAGE) ./server
	@echo "Saving server image to tar for kind..."
	podman save -o mern-server.tar $(SERVER_IMAGE)
	@echo "Applying manifests..."
	kubectl apply -f $(MANIFESTS)
	@echo "show pods"
	kubectl get pods

ingress-test:
	@echo "test ingress"
	kubectl get ingress mern-ingress



hey-npm:
	@echo "save valid Accesstoken in TOKEN= (tip: copy from local storage json)"
	hey -n 1000 -c 500 \
	-H "Authorization: Bearer $TOKEN" \
	http://localhost:3000/home > hey/hey-results-npm.txt
	cat hey/hey-results-npm.txt

hey-local:
	@echo "save valid Accesstoken in TOKEN= (tip: copy from local storage json)"
	hey -n 1000 -c 500 \
	-H "Authorization: Bearer $TOKEN" \
	http://localhost:3000/home > hey/hey-results-local.txt
	cat hey/hey-results-local.txt

hey-public:
	@echo "save valid Accesstoken in TOKEN= (tip: copy from local storage json)"
	hey -n 1000 -c 500 \
	-H "Authorization: Bearer $TOKEN" \
	http://localhost:8080/home > hey/hey-results-public.txt
	cat hey/hey-results-public.txt



start-dev:
	@echo "start dev environment"
	podman start local-mongo
	@echo "cd client : npm run start"
	@echo "cd client : npm run start"