## Nexus Management
## Nexus Repository Manager via helmfile (no operator)

include common/Makefile

# Namespace (now single namespace)
NEXUS_NAMESPACE := tech-nexus

# Helmfile
NEXUS_HELMFILE := nexus/helmfile.yaml

# Manifests
NEXUS_MANIFESTS_DIR := nexus/manifests
NEXUS_NAMESPACE_MANIFEST := $(NEXUS_MANIFESTS_DIR)/namespace.yaml
NEXUS_LIMITRANGE_MANIFEST := $(NEXUS_MANIFESTS_DIR)/limitrange.yaml
NEXUS_RESOURCEQUOTA_MANIFEST := $(NEXUS_MANIFESTS_DIR)/resourcequota.yaml
NEXUS_STORAGECLASS_MANIFEST := $(NEXUS_MANIFESTS_DIR)/storageclass.yaml
NEXUS_INGRESS_MANIFEST := $(NEXUS_MANIFESTS_DIR)/ingress.yaml

.PHONY: nexus-help
nexus-help:
	@echo ""
	@echo "$(COLOR_BOLD)Nexus Commands:$(COLOR_RESET)"
	@echo "  make nexus-install      - Install Nexus via helmfile"
	@echo "  make nexus-update       - Update Nexus via helmfile"
	@echo "  make nexus-uninstall    - Uninstall Nexus (helmfile destroy)"
	@echo ""
	@echo "  make nexus-apply-policy - Apply LimitRange/ResourceQuota/StorageClass"
	@echo "  make nexus-apply-ingress - Apply Ingress (optional)"
	@echo "  make nexus-delete-ingress - Delete Ingress"
	@echo ""
	@echo "  make nexus-status       - Check Nexus status"
	@echo ""
	@echo "$(COLOR_YELLOW)Note:$(COLOR_RESET) Before installing:"
	@echo "  1) Prepare nodes: cd ../.. && make k8s-prepare-nodes"
	@echo "  2) Make sure Longhorn is healthy (if using it for PVC)"
	@echo ""

# -------------------------------------------------------------------
# Policies / prerequisites
# -------------------------------------------------------------------

.PHONY: nexus-apply-policy
nexus-apply-policy: install-namespaces
	$(call print_info,Applying namespace $(NEXUS_NAMESPACE)...)
	@$(KUBECTL) apply -f $(NEXUS_NAMESPACE_MANIFEST)
	$(call print_info,Applying Nexus resource policies...)
	@$(KUBECTL) apply -f $(NEXUS_LIMITRANGE_MANIFEST)
	@$(KUBECTL) apply -f $(NEXUS_RESOURCEQUOTA_MANIFEST)
	$(call print_info,Applying StorageClass for Nexus (if used)...)
	@$(KUBECTL) apply -f $(NEXUS_STORAGECLASS_MANIFEST)
	$(call print_success,Policies applied!)

# -------------------------------------------------------------------
# Install / Update / Uninstall
# -------------------------------------------------------------------

.PHONY: nexus-install
nexus-install: nexus-apply-policy
	$(call print_info,Installing Nexus via helmfile...)
	$(call run_helmfile,$(NEXUS_HELMFILE),sync)
	$(call print_info,Waiting for Nexus pod to be ready...)
	$(call sleep_seconds,20)
	@$(KUBECTL) -n $(NEXUS_NAMESPACE) wait --for=condition=ready pod -l app.kubernetes.io/instance=nexus-tech --timeout=10m || \
	  $(KUBECTL) -n $(NEXUS_NAMESPACE) wait --for=condition=ready pod -l app.kubernetes.io/name=nexus3 --timeout=10m || \
	  $(KUBECTL) -n $(NEXUS_NAMESPACE) wait --for=condition=ready pod -l app.kubernetes.io/name=nexus --timeout=10m
	$(call print_success,Nexus installed!)
	@echo ""
	@echo "$(COLOR_YELLOW)Tip:$(COLOR_RESET) Monitor with: make nexus-status"

.PHONY: nexus-update
nexus-update: nexus-apply-policy
	$(call print_info,Updating Nexus via helmfile...)
	$(call run_helmfile,$(NEXUS_HELMFILE),sync)
	$(call print_info,Waiting for Nexus pod to be ready...)
	$(call sleep_seconds,20)
	@$(KUBECTL) -n $(NEXUS_NAMESPACE) wait --for=condition=ready pod -l app.kubernetes.io/instance=nexus-tech --timeout=10m || true
	$(call print_success,Nexus updated!)

.PHONY: nexus-uninstall
nexus-uninstall:
	$(call print_warning,This will uninstall Nexus!)
	@echo "$(COLOR_YELLOW)Note:$(COLOR_RESET) PVC data may remain depending on reclaimPolicy."
	@echo "Press Ctrl+C to cancel, or wait 10 seconds to continue..."
	$(call sleep_seconds,10)
	$(call print_info,Uninstalling Nexus via helmfile...)
	$(call run_helmfile,$(NEXUS_HELMFILE),destroy) || true
	$(call print_success,Nexus uninstalled.)

# -------------------------------------------------------------------
# Ingress (optional, keep it as plain manifests like your other stacks)
# -------------------------------------------------------------------

.PHONY: nexus-apply-ingress
nexus-apply-ingress:
	$(call print_info,Applying Nexus Ingress (optional)...)
	@$(KUBECTL) apply -f $(NEXUS_INGRESS_MANIFEST)
	$(call print_success,Ingress applied!)

.PHONY: nexus-delete-ingress
nexus-delete-ingress:
	$(call print_warning,Deleting Nexus Ingress...)
	@$(KUBECTL) delete -f $(NEXUS_INGRESS_MANIFEST) --ignore-not-found=true || true
	$(call print_success,Ingress deleted (if existed).)

# -------------------------------------------------------------------
# Status
# -------------------------------------------------------------------

.PHONY: nexus-status
nexus-status:
	@echo ""
	@echo "$(COLOR_BOLD)=== Nexus Status ($(NEXUS_NAMESPACE)) ===$(COLOR_RESET)"
	@$(KUBECTL) -n $(NEXUS_NAMESPACE) get pods -o wide
	@echo ""
	@echo "$(COLOR_BOLD)=== Services ===$(COLOR_RESET)"
	@$(KUBECTL) -n $(NEXUS_NAMESPACE) get svc -o wide
	@echo ""
	@echo "$(COLOR_BOLD)=== PVC ===$(COLOR_RESET)"
	@$(KUBECTL) -n $(NEXUS_NAMESPACE) get pvc
	@echo ""
	@echo "$(COLOR_BOLD)=== Recent logs (last 30 lines) ===$(COLOR_RESET)"
	@$(KUBECTL) -n $(NEXUS_NAMESPACE) logs -l app.kubernetes.io/instance=nexus-tech --tail=30 2>&1 || \
	  $(KUBECTL) -n $(NEXUS_NAMESPACE) logs -l app.kubernetes.io/name=nexus3 --tail=30 2>&1 || \
	  $(KUBECTL) -n $(NEXUS_NAMESPACE) logs -l app.kubernetes.io/name=nexus --tail=30 2>&1 || \
	  echo "No logs available"
	@echo ""
