# Image URL to use all building/pushing image targets
IMG ?= viettel/ai-platform/modelregistry-operator:latest
# Produce CRDs that work back to Kubernetes 1.16
CRD_OPTIONS ?= "crd"

# Get the currently used golang install path (in GOPATH/bin, unless ROOT_DIR is changed)
ifeq (,$(shell go env GOBIN))
GOBIN=$(shell go env GOPATH)/bin
else
GOBIN=$(shell go env GOBIN)
endif

# Setting SHELL to bash allows bash commands to be executed by recipes in this Makefile;
# values for shell options: set [-e], set [-o], set [-e] set [-o pipefail]
SHELL = /usr/bin/env bash -o pipefail
.SHELLFLAGS = -ec

.PHONY: all
all: manager

##@ General

# The help target prints out all targets with their descriptions organized
# beneath their categories. The categories are represented by ##@ and the target
# descriptions by ###. Binary name(s) multiple targets can also be added to
# target description with extra two spaces.
## Usage:
## make help

.PHONY: help
help: ## Display this help
	@awk 'BEGIN {FS = ":.*##"; printf "\nUsage:\n  make \033[36m<target>\033[0m\n"} /^[0-9a-zA-Z_-]+:.*?##/ {printf "  \033[36m%-15s\033[0m %s\n", $$1, $$2}' $(MAKEFILE_LIST)

##@ Build

.PHONY: build
build: fmt vet ## Build manager binary
	go build -o bin/manager ./main.go

.PHONY: run
run: ## Run against the configured Kubernetes cluster in ~/.kube/config
	go run ./main.go

##@ Deployment

.PHONY: docker-build
docker-build: ## Build docker image with the manager
	docker build -t $(IMG) .

.PHONY: docker-push
docker-push: ## Push docker image with the manager
	docker push $(IMG)

##@ Dependencies

.PHONY: deps
deps: go-mod-download

.PHONY: go-mod-download
go-mod-download: ## Download go module dependencies
	go mod download

.PHONY: tidy
tidy: ## Run go mod tidy
	go mod tidy

.PHONY: verify
verify: deps tidy ## Verify code
	@if ! git diff --exit-code; then \
		echo "Changes detected. Run 'make tidy' to update files."; \
		exit 1; \
	fi

##@ Testing

.PHONY: test
test: ## Run unit tests
	go test ./... -v

.PHONY: coverage
coverage: ## Run unit tests with coverage
	go test ./... -coverprofile cover.out
	go tool cover -html=cover.out -o coverage.html

.PHONY: lint
lint: ## Run golangci-lint
	@if ! command -v golangci-lint &> /dev/null; then \
		echo "Installing golangci-lint..."; \
		go install github.com/golangci/golangci-lint/cmd/golangci-lint@latest; \
	fi
	golangci-lint run ./...

.PHONY: lint-deadcode
lint-deadcode: ## Run deadcode detection only
	@if ! command -v golangci-lint &> /dev/null; then \
		echo "Installing golangci-lint..."; \
		go install github.com/golangci/golangci-lint/cmd/golangci-lint@latest; \
	fi
	golangci-lint run --enable=deadcode,unused ./...

.PHONY: lint-all
lint-all: ## Run all linters
	$(MAKE) lint-deadcode
	$(MAKE) lint

.PHONY: ci-check
ci-check: ## Run all CI checks
	$(MAKE) fmt
	$(MAKE) vet
	$(MAKE) lint-all
	$(MAKE) test

##@ Code Inspection

.PHONY: inspect
inspect: ## Run GoLand code inspection from CLI
	@echo "Running GoLand code inspection..."
	@./scripts/run-inspection.sh

.PHONY: inspect-report
inspect-report: inspect ## Show inspection results summary (alias for inspect)
	@# Results are displayed by the inspect target

.PHONY: inspect-ci
inspect-ci: ## Run GoLand inspection (CI mode - no output, just results)
	@killall GoLand 2>/dev/null || true
	@sleep 1
	@/Applications/GoLand.app/Contents/bin/inspect.sh \
	  . \
	  .idea/inspectionProfiles/Go_Comprehensive.xml \
	  inspection-results \
	  -v0 \
	  -format xml
	@echo "Inspection complete. Results in inspection-results/"

.PHONY: fmt
fmt: ## Run go fmt
	go fmt ./...

.PHONY: vet
vet: ## Run go vet
	go vet ./...

.PHONY: generate
generate: ## Run controller-gen to generate code
	@if ! command -v controller-gen &> /dev/null; then \
		echo "Installing controller-gen..."; \
		go install sigs.k8s.io/controller-tools/cmd/controller-gen@latest; \
	fi
	controller-gen object:headerFile="hack/boilerplate.go.txt" paths="./..."

.PHONY: manifests
manifests: ## Generate CRD manifests
	@if ! command -v controller-gen &> /dev/null; then \
		echo "Installing controller-gen..."; \
		go install sigs.k8s.io/controller-tools/cmd/controller-gen@latest; \
	fi
	controller-gen rbac:roleName=manager-role crd webhook paths="./..." output:crd:artifacts:config=config/crd/bases

##@ Cleanup

.PHONY: clean
clean: ## Clean build artifacts
	rm -rf bin/
	rm -f cover.out coverage.html
