## SonarQube Management
## SonarQube через официальный Helm chart

# Include common variables and functions
include common/Makefile

# SonarQube configuration
# Using common namespace variables from common/Makefile

.PHONY: sonarqube-help
sonarqube-help:
	@echo ""
	@echo "$(COLOR_BOLD)SonarQube Commands:$(COLOR_RESET)"
	@echo "  make sonarqube-install         - Install SonarQube"
	@echo "  make sonarqube-update         - Update SonarQube with new values"
	@echo "  make sonarqube-uninstall       - Uninstall SonarQube (keeps data)"
	@echo "  make sonarqube-delete          - Delete SonarQube and data"
	@echo "  make sonarqube-create-secret   - Create PostgreSQL password secret"
	@echo "  make sonarqube-update-secret   - Update PostgreSQL password secret"
	@echo "  make sonarqube-delete-secret   - Delete PostgreSQL password secret"
	@echo "  make sonarqube-get-secret      - Get PostgreSQL password secret info"
	@echo "  make sonarqube-create-monitoring-secret - Create monitoring passcode secret"
	@echo "  make sonarqube-update-monitoring-secret - Update monitoring passcode secret"
	@echo "  make sonarqube-delete-monitoring-secret - Delete monitoring passcode secret"
	@echo "  make sonarqube-get-monitoring-secret - Get monitoring passcode secret info"
	@echo "  make sonarqube-status          - Check SonarQube status"
	@echo ""
	@echo "$(COLOR_YELLOW)Note:$(COLOR_RESET) Before installing:"
	@echo "  1. PostgreSQL cluster pg-public must be running"
	@echo "  2. Create PostgreSQL secret: make sonarqube-create-secret"
	@echo "  3. Create monitoring secret: make sonarqube-create-monitoring-secret"
	@echo "  4. Prepare nodes: cd ../.. && make k8s-prepare-nodes"

.PHONY: sonarqube-check-longhorn
sonarqube-check-longhorn:
	$(call check_longhorn)

.PHONY: sonarqube-check-postgres
sonarqube-check-postgres:
	$(call check_postgres_cluster)

.PHONY: sonarqube-create-secret
sonarqube-create-secret:
	$(call print_info,Creating namespace $(SONARQUBE_NAMESPACE) if not exists...)
	@$(KUBECTL) apply -f sonarqube/manifests/namespace.yaml
	$(call print_info,Creating secret sonarqube-postgres-secret in $(SONARQUBE_NAMESPACE)...)
	@echo "$(COLOR_YELLOW)Enter PostgreSQL password for SonarQube user:$(COLOR_RESET)"
ifeq ($(DETECTED_OS),Windows)
	@powershell -NoProfile -Command "$$passSecure = Read-Host -AsSecureString -Prompt '> '; $$pass = [Runtime.InteropServices.Marshal]::PtrToStringAuto([Runtime.InteropServices.Marshal]::SecureStringToBSTR($$passSecure)); $$curDir = [System.IO.Directory]::GetCurrentDirectory(); $$kubeconfig = [System.IO.Path]::GetFullPath([System.IO.Path]::Combine($$curDir, '$(KUBECONFIG_REL)')); $$env:KUBECONFIG = $$kubeconfig; kubectl -n $(SONARQUBE_NAMESPACE) delete secret sonarqube-postgres-secret --ignore-not-found=true 2>$$null; kubectl -n $(SONARQUBE_NAMESPACE) create secret generic sonarqube-postgres-secret --from-literal=password=$$pass"
else
	@read -s -p "> " pass && \
		$(KUBECTL) -n $(SONARQUBE_NAMESPACE) delete secret sonarqube-postgres-secret --ignore-not-found=true 2>/dev/null || true && \
		$(KUBECTL) -n $(SONARQUBE_NAMESPACE) create secret generic sonarqube-postgres-secret --from-literal=password=$$pass
endif
	$(call print_success,Secret created!)
	@echo ""
	@echo "$(COLOR_YELLOW)Note:$(COLOR_RESET) Make sure the PostgreSQL user 'sonarqube' exists in pg-public cluster"
	@echo "  Connect to PostgreSQL and create user:"
	@echo "    CREATE USER sonarqube WITH PASSWORD 'your_password';"
	@echo "    CREATE DATABASE sonarqube OWNER sonarqube;"
	@echo "    GRANT ALL PRIVILEGES ON DATABASE sonarqube TO sonarqube;"

.PHONY: sonarqube-update-secret
sonarqube-update-secret:
	$(call print_info,Updating secret sonarqube-postgres-secret in $(SONARQUBE_NAMESPACE)...)
	@echo "$(COLOR_YELLOW)Enter PostgreSQL password for SonarQube user:$(COLOR_RESET)"
ifeq ($(DETECTED_OS),Windows)
	@powershell -NoProfile -Command "$$passSecure = Read-Host -AsSecureString -Prompt '> '; $$pass = [Runtime.InteropServices.Marshal]::PtrToStringAuto([Runtime.InteropServices.Marshal]::SecureStringToBSTR($$passSecure)); $$curDir = [System.IO.Directory]::GetCurrentDirectory(); $$kubeconfig = [System.IO.Path]::GetFullPath([System.IO.Path]::Combine($$curDir, '$(KUBECONFIG_REL)')); $$env:KUBECONFIG = $$kubeconfig; kubectl -n $(SONARQUBE_NAMESPACE) delete secret sonarqube-postgres-secret --ignore-not-found=true 2>$$null; kubectl -n $(SONARQUBE_NAMESPACE) create secret generic sonarqube-postgres-secret --from-literal=password=$$pass"
else
	@read -s -p "> " pass && \
		$(KUBECTL) -n $(SONARQUBE_NAMESPACE) delete secret sonarqube-postgres-secret --ignore-not-found=true 2>/dev/null || true && \
		$(KUBECTL) -n $(SONARQUBE_NAMESPACE) create secret generic sonarqube-postgres-secret --from-literal=password=$$pass
endif
	$(call print_success,Secret updated!)

.PHONY: sonarqube-delete-secret
sonarqube-delete-secret:
	$(call print_warning,This will delete the sonarqube-postgres-secret secret!)
	@echo "Press Ctrl+C to cancel, or wait 5 seconds to continue..."
	$(call sleep_seconds,5)
	$(call print_info,Deleting secret sonarqube-postgres-secret...)
	-@$(KUBECTL) -n $(SONARQUBE_NAMESPACE) delete secret sonarqube-postgres-secret || true
	$(call print_success,Secret deleted!)

.PHONY: sonarqube-get-secret
sonarqube-get-secret:
	@echo ""
	@echo "$(COLOR_BOLD)=== SonarQube PostgreSQL Secret Info ===$(COLOR_RESET)"
	@$(KUBECTL) -n $(SONARQUBE_NAMESPACE) get secret sonarqube-postgres-secret >/dev/null 2>&1 && \
		($(call print_success,Secret sonarqube-postgres-secret exists) && \
		 echo "$(COLOR_YELLOW)Secret keys:$(COLOR_RESET)" && \
		 echo "$(COLOR_YELLOW)Secret keys:$(COLOR_RESET)" && \
		 $(call get_secret_keys,$(SONARQUBE_NAMESPACE),sonarqube-postgres-secret)) || \
		($(call print_warning,Secret sonarqube-postgres-secret not found))
	@echo ""

.PHONY: sonarqube-create-monitoring-secret
sonarqube-create-monitoring-secret:
	$(call print_info,Creating namespace $(SONARQUBE_NAMESPACE) if not exists...)
	@$(KUBECTL) apply -f sonarqube/manifests/namespace.yaml
	$(call print_info,Creating secret sonarqube-monitoring-secret in $(SONARQUBE_NAMESPACE)...)
	@echo "$(COLOR_YELLOW)Enter monitoring passcode for SonarQube (used for health checks):$(COLOR_RESET)"
ifeq ($(DETECTED_OS),Windows)
	@powershell -NoProfile -Command "$$passSecure = Read-Host -AsSecureString -Prompt '> '; $$pass = [Runtime.InteropServices.Marshal]::PtrToStringAuto([Runtime.InteropServices.Marshal]::SecureStringToBSTR($$passSecure)); $$curDir = [System.IO.Directory]::GetCurrentDirectory(); $$kubeconfig = [System.IO.Path]::GetFullPath([System.IO.Path]::Combine($$curDir, '$(KUBECONFIG_REL)')); $$env:KUBECONFIG = $$kubeconfig; kubectl -n $(SONARQUBE_NAMESPACE) delete secret sonarqube-monitoring-secret --ignore-not-found=true 2>$$null; kubectl -n $(SONARQUBE_NAMESPACE) create secret generic sonarqube-monitoring-secret --from-literal=passcode=$$pass"
else
	@read -s -p "> " pass && \
		$(KUBECTL) -n $(SONARQUBE_NAMESPACE) delete secret sonarqube-monitoring-secret --ignore-not-found=true 2>/dev/null || true && \
		$(KUBECTL) -n $(SONARQUBE_NAMESPACE) create secret generic sonarqube-monitoring-secret --from-literal=passcode=$$pass
endif
	$(call print_success,Secret created!)

.PHONY: sonarqube-update-monitoring-secret
sonarqube-update-monitoring-secret:
	$(call print_info,Updating secret sonarqube-monitoring-secret in $(SONARQUBE_NAMESPACE)...)
	@echo "$(COLOR_YELLOW)Enter monitoring passcode for SonarQube (used for health checks):$(COLOR_RESET)"
ifeq ($(DETECTED_OS),Windows)
	@powershell -NoProfile -Command "$$passSecure = Read-Host -AsSecureString -Prompt '> '; $$pass = [Runtime.InteropServices.Marshal]::PtrToStringAuto([Runtime.InteropServices.Marshal]::SecureStringToBSTR($$passSecure)); $$curDir = [System.IO.Directory]::GetCurrentDirectory(); $$kubeconfig = [System.IO.Path]::GetFullPath([System.IO.Path]::Combine($$curDir, '$(KUBECONFIG_REL)')); $$env:KUBECONFIG = $$kubeconfig; kubectl -n $(SONARQUBE_NAMESPACE) delete secret sonarqube-monitoring-secret --ignore-not-found=true 2>$$null; kubectl -n $(SONARQUBE_NAMESPACE) create secret generic sonarqube-monitoring-secret --from-literal=passcode=$$pass"
else
	@read -s -p "> " pass && \
		$(KUBECTL) -n $(SONARQUBE_NAMESPACE) delete secret sonarqube-monitoring-secret --ignore-not-found=true 2>/dev/null || true && \
		$(KUBECTL) -n $(SONARQUBE_NAMESPACE) create secret generic sonarqube-monitoring-secret --from-literal=passcode=$$pass
endif
	$(call print_success,Secret updated!)

.PHONY: sonarqube-delete-monitoring-secret
sonarqube-delete-monitoring-secret:
	$(call print_warning,This will delete the sonarqube-monitoring-secret secret!)
	@echo "Press Ctrl+C to cancel, or wait 5 seconds to continue..."
	$(call sleep_seconds,5)
	$(call print_info,Deleting secret sonarqube-monitoring-secret...)
	-@$(KUBECTL) -n $(SONARQUBE_NAMESPACE) delete secret sonarqube-monitoring-secret || true
	$(call print_success,Secret deleted!)

.PHONY: sonarqube-get-monitoring-secret
sonarqube-get-monitoring-secret:
	@echo ""
	@echo "$(COLOR_BOLD)=== SonarQube Monitoring Secret Info ===$(COLOR_RESET)"
	@$(KUBECTL) -n $(SONARQUBE_NAMESPACE) get secret sonarqube-monitoring-secret >/dev/null 2>&1 && \
		($(call print_success,Secret sonarqube-monitoring-secret exists) && \
		 echo "$(COLOR_YELLOW)Secret keys:$(COLOR_RESET)" && \
		 echo "$(COLOR_YELLOW)Secret keys:$(COLOR_RESET)" && \
		 $(call get_secret_keys,$(SONARQUBE_NAMESPACE),sonarqube-monitoring-secret)) || \
		($(call print_warning,Secret sonarqube-monitoring-secret not found))
	@echo ""

.PHONY: sonarqube-install
sonarqube-install: install-namespaces sonarqube-check-longhorn sonarqube-check-postgres
	@echo ""
	@echo "$(COLOR_YELLOW)Note:$(COLOR_RESET) Make sure secrets are created:"
	@echo "  make sonarqube-create-secret (PostgreSQL password)"
	@echo "  make sonarqube-create-monitoring-secret (monitoring passcode)"
	@echo ""
	@$(KUBECTL) -n $(SONARQUBE_NAMESPACE) get secret sonarqube-postgres-secret || \
		($(call print_warning,SonarQube PostgreSQL password secret not found!) && \
		 echo "$(COLOR_YELLOW)Please create secret first:$(COLOR_RESET)" && \
		 echo "  make sonarqube-create-secret" && \
		 exit 1)
	@$(KUBECTL) -n $(SONARQUBE_NAMESPACE) get secret sonarqube-monitoring-secret || \
		($(call print_warning,SonarQube monitoring passcode secret not found!) && \
		 echo "$(COLOR_YELLOW)Please create secret first:$(COLOR_RESET)" && \
		 echo "  make sonarqube-create-monitoring-secret" && \
		 exit 1)
	$(call print_info,Applying SonarQube resource policies...)
	@$(KUBECTL) apply -f sonarqube/manifests/limitrange.yaml
	@$(KUBECTL) apply -f sonarqube/manifests/resourcequota.yaml
	$(call print_info,Creating StorageClass for SonarQube...)
	@$(KUBECTL) apply -f sonarqube/manifests/storageclass.yaml
	$(call print_info,Installing SonarQube via helmfile...)
	$(call run_helmfile,sonarqube/helmfile.yaml,sync)
	$(call print_info,Waiting for SonarQube to be ready...)
	$(call sleep_seconds,30)
	@$(KUBECTL) -n $(SONARQUBE_NAMESPACE) wait --for=condition=ready pod -l app=sonarqube --timeout=10m || \
		$(KUBECTL) -n $(SONARQUBE_NAMESPACE) wait --for=condition=ready pod -l app.kubernetes.io/name=sonarqube --timeout=10m || true
	$(call print_success,SonarQube installed!)
	@echo ""
	@echo "$(COLOR_YELLOW)Note:$(COLOR_RESET) SonarQube may take 2-5 minutes to fully start"
	@echo "Monitor with: make sonarqube-status"

.PHONY: sonarqube-update
sonarqube-update:
	$(call print_info,Updating SonarQube resource policies...)
	@$(KUBECTL) apply -f sonarqube/manifests/limitrange.yaml
	@$(KUBECTL) apply -f sonarqube/manifests/resourcequota.yaml
	$(call print_info,Updating SonarQube with new values via helmfile...)
	$(call run_helmfile,sonarqube/helmfile.yaml,sync)
	$(call print_info,Waiting for SonarQube to be ready...)
	$(call sleep_seconds,30)
	@$(KUBECTL) -n $(SONARQUBE_NAMESPACE) wait --for=condition=ready pod -l app=sonarqube --timeout=10m || \
		$(KUBECTL) -n $(SONARQUBE_NAMESPACE) wait --for=condition=ready pod -l app.kubernetes.io/name=sonarqube --timeout=10m || true
	$(call print_success,SonarQube updated!)
	@echo ""
	@echo "$(COLOR_YELLOW)Note:$(COLOR_RESET) SonarQube pods will restart with new configuration"

.PHONY: sonarqube-uninstall
sonarqube-uninstall:
	$(call print_warning,This will uninstall SonarQube but keep the data PVC!)
	@echo "Press Ctrl+C to cancel, or wait 10 seconds to continue..."
	$(call sleep_seconds,10)
	$(call print_info,Uninstalling SonarQube...)
	$(call run_helmfile,sonarqube/helmfile.yaml,destroy) || true
	$(call print_success,SonarQube uninstalled. Data PVC is preserved.)

.PHONY: sonarqube-delete
sonarqube-delete:
	$(call print_warning,This will delete SonarQube and ALL its data!)
	@echo "Press Ctrl+C to cancel, or wait 10 seconds to continue..."
	$(call sleep_seconds,10)
	$(call print_info,Uninstalling SonarQube...)
	$(call run_helmfile,sonarqube/helmfile.yaml,destroy) || true
	$(call print_info,Deleting SonarQube PVCs...)
	-@$(KUBECTL) -n $(SONARQUBE_NAMESPACE) delete pvc -l app=sonarqube || \
		$(KUBECTL) -n $(SONARQUBE_NAMESPACE) delete pvc -l app.kubernetes.io/name=sonarqube || true
	$(call print_success,SonarQube and data deleted.)

.PHONY: sonarqube-status
sonarqube-status:
	@echo ""
	@echo "$(COLOR_BOLD)=== SonarQube Status ===$(COLOR_RESET)"
	@$(KUBECTL) -n $(SONARQUBE_NAMESPACE) get pods -o wide
	@echo ""
	@echo "$(COLOR_BOLD)=== SonarQube Services ===$(COLOR_RESET)"
	@$(KUBECTL) -n $(SONARQUBE_NAMESPACE) get svc
	@echo ""
	@echo "$(COLOR_BOLD)=== SonarQube PVCs ===$(COLOR_RESET)"
	@$(KUBECTL) -n $(SONARQUBE_NAMESPACE) get pvc
	@echo ""
	@echo "$(COLOR_BOLD)=== SonarQube Pod Logs (last 20 lines) ===$(COLOR_RESET)"
	@$(KUBECTL) -n $(SONARQUBE_NAMESPACE) logs -l app=sonarqube --tail=20 2>&1 || \
		$(KUBECTL) -n $(SONARQUBE_NAMESPACE) logs -l app.kubernetes.io/name=sonarqube --tail=20 2>&1 || \
		echo "No logs available"
