IMAGE ?= matst80/gibbz:latest
NAMESPACE ?= home
VERSION ?= $(shell git describe --tags --exact-match 2>/dev/null || git rev-parse --short=12 HEAD)
GIT_COMMIT ?= $(shell git rev-parse HEAD)
BUILD_DATE ?= $(shell date -u +"%Y-%m-%dT%H:%M:%SZ")

.PHONY: all help build test check docker-build docker-push deploy rollout status logs

all: check

help: ## Show target help
	@grep -E '^[a-zA-Z_-]+:.*?## ' $(MAKEFILE_LIST) | awk 'BEGIN{FS=":.*?## "}{printf "  \033[36m%-16s\033[0m %s\n", $$1, $$2}'

test: ## Run local tests
	npm test

check: test ## Quality gate: run all tests

build: check ## Build local dependencies and run tests
	npm ci

docker-build: ## Build docker image
	docker build \
		--build-arg APP_RELEASE=$(VERSION) \
		--build-arg VERSION=$(VERSION) \
		--build-arg GIT_COMMIT=$(GIT_COMMIT) \
		--build-arg BUILD_DATE=$(BUILD_DATE) \
		-t $(IMAGE) \
		.

docker-push: ## Push docker image
	docker push $(IMAGE)

deploy: ## Apply Kubernetes manifests to the cluster
	kubectl apply -f k8s/deployment.yaml

rollout: ## Trigger rolling restart of the deployment
	kubectl rollout restart deployment/gibbz -n $(NAMESPACE)
	kubectl rollout status deployment/gibbz -n $(NAMESPACE)

status: ## Check deployment and pod status
	kubectl get pods,svc,ingress -l app=gibbz -n $(NAMESPACE)

logs: ## Tail pod logs
	kubectl logs -l app=gibbz -n $(NAMESPACE) -f
