export GOPRIVATE := github.com/tidbcloud/*
MAKEFILE_DIR:=$(shell dirname $(realpath $(firstword $(MAKEFILE_LIST))))
GOLANGCI_LINT_VERSION:=v2.11.4
GOTESTSUM_VERSION:=v1.13.0
GO_UNIT_TEST_FLAGS ?= -v -failfast -shuffle=on
GO_INTEGRATION_TEST_FLAGS ?= -v
GO_FUNCTIONAL_TEST_FLAGS ?= -v -count=1 -timeout=12h
GO_FUNCTIONAL_TEST_RUN ?= ^TestFunctional_
GO_FUNCTIONAL_TEST_PACKAGES ?= ./...
GOLANGCI_LINT_BIN := golangci-lint
ifeq ($(shell command -v $(GOLANGCI_LINT_BIN) >/dev/null 2>&1; echo $$?),1)
  GOLANGCI_LINT := GOPRIVATE=$(GOPRIVATE) go run github.com/golangci/golangci-lint/v2/cmd/golangci-lint@$(GOLANGCI_LINT_VERSION)
else
  GOLANGCI_LINT := $(GOLANGCI_LINT_BIN)
endif

GOTESTSUM_BIN := gotestsum
GOTESTSUM_CACHE := $(MAKEFILE_DIR)/.cache/gotestsum/gotestsum
ifeq ($(wildcard $(GOTESTSUM_CACHE)),)
ifeq ($(shell command -v $(GOTESTSUM_BIN) >/dev/null 2>&1; echo $$?),0)
  GOTESTSUM := $(GOTESTSUM_BIN)
else
  GOTESTSUM := GOPRIVATE=$(GOPRIVATE) go run gotest.tools/gotestsum@$(GOTESTSUM_VERSION)
endif
else
  GOTESTSUM := $(GOTESTSUM_CACHE)
endif

default: dev

.PHONY: dev
dev: generate mod fmt build test

.PHONY: vet
vet:
	GOPRIVATE=$(GOPRIVATE) go vet ./...

.PHONY: mod
mod:
	GOPRIVATE=$(GOPRIVATE) go mod tidy
	GOPRIVATE=$(GOPRIVATE) go mod download # refer to https://github.com/golang/go/issues/44435#issuecomment-947950424

.PHONY: generate
generate:
	GOPRIVATE=$(GOPRIVATE) go generate ./...

.PHONY: fmt
fmt:
	$(GOLANGCI_LINT) run --config $(MAKEFILE_DIR)/.golangci.yml --fix

.PHONY: lint
lint:
	$(GOLANGCI_LINT) run --config $(MAKEFILE_DIR)/.golangci.yml

.PHONY: test
test:
	$(GOTESTSUM) --format=testname -- $(GO_UNIT_TEST_FLAGS) ./...

.PHONY: test-race
test-race:
	$(GOTESTSUM) --format=testname -- $(GO_UNIT_TEST_FLAGS) -race ./...

.PHONY: test-coverage
test-coverage:
	$(GOTESTSUM) --format=testname -- $(GO_UNIT_TEST_FLAGS) -coverprofile=$(MAKEFILE_DIR)/coverage.txt ./...

.PHONY: test-integration
test-integration:
	$(GOTESTSUM) --format=testname -- $(GO_INTEGRATION_TEST_FLAGS) -tags integration -run TestIntegration_ ./...

.PHONY: test-integration-race
test-integration-race:
	$(GOTESTSUM) --format=testname -- $(GO_INTEGRATION_TEST_FLAGS) -race -tags integration -run TestIntegration_ ./...

# Final behavior functional tests are deliberately excluded from test and
# test-integration. They compile only through this explicit target and require
# each test's own mutation opt-in.
#
# GO_FUNCTIONAL_TEST_FLAGS disables caching and gives the ten-hour proof enough
# watchdog headroom for cleanup. See spec-tests/README.md for test selection
# and test-specific inputs, cleanup, and verdict semantics.
.PHONY: test-functional
test-functional:
	GOPRIVATE=$(GOPRIVATE) go test $(GO_FUNCTIONAL_TEST_FLAGS) -tags functional -run '$(GO_FUNCTIONAL_TEST_RUN)' $(GO_FUNCTIONAL_TEST_PACKAGES)

.PHONY: build
build:
	mkdir -p $(MAKEFILE_DIR)/bin
	GOPRIVATE=$(GOPRIVATE) go build -o $(MAKEFILE_DIR)/bin/cp9 ./cmd/cp9/

.PHONY: dbaas-ui-candidate
dbaas-ui-candidate:
	$(MAKEFILE_DIR)/build/dbaas-ui/candidate.sh
