BINARY  := terraform-provider-dokploy
VERSION ?= 0.1.0
NAMESPACE := maxvojtkov
NAME      := dokploy

OS_ARCH := $(shell go env GOOS)_$(shell go env GOARCH)
MIRROR  := $(HOME)/.local/share/terraform/plugins
DEST    := $(MIRROR)/registry.terraform.io/$(NAMESPACE)/$(NAME)/$(VERSION)/$(OS_ARCH)

.PHONY: build
build: ## Compile the provider
	go build -ldflags "-X main.version=$(VERSION)" -o $(BINARY) .

.PHONY: install
install: ## Build and install into the local filesystem mirror
	@mkdir -p "$(DEST)"
	go build -ldflags "-X main.version=$(VERSION)" -o "$(DEST)/$(BINARY)_v$(VERSION)" .
	@echo
	@echo "Installed $(BINARY) $(VERSION) to:"
	@echo "  $(DEST)"
	@echo
	@echo "Add this to ~/.terraformrc (or ~/.tofurc):"
	@echo
	@echo '  provider_installation {'
	@echo '    filesystem_mirror {'
	@echo '      path    = "$(MIRROR)"'
	@echo '      include = ["registry.terraform.io/$(NAMESPACE)/$(NAME)"]'
	@echo '    }'
	@echo '    direct {'
	@echo '      exclude = ["registry.terraform.io/$(NAMESPACE)/$(NAME)"]'
	@echo '    }'
	@echo '  }'

.PHONY: test
test: ## Run unit tests
	go test ./... -count=1

.PHONY: testacc
testacc: ## Run acceptance tests against a live instance (needs DOKPLOY_HOST and DOKPLOY_API_KEY)
	TF_ACC=1 go test ./internal/provider/ -v -count=1 -timeout 30m

.PHONY: docs
docs: ## Regenerate docs/ from the provider schema
	go run github.com/hashicorp/terraform-plugin-docs/cmd/tfplugindocs@v0.23.0 generate --provider-name dokploy

.PHONY: fmt
fmt: ## Format Go and Terraform sources
	gofmt -w .
	@command -v terraform >/dev/null && terraform fmt -recursive examples modules || true

.PHONY: lint
lint: ## Vet Go sources and check formatting
	go vet ./...
	@test -z "$$(gofmt -l .)" || { echo "gofmt needed:"; gofmt -l .; exit 1; }

.PHONY: clean
clean: ## Remove build artifacts
	rm -f $(BINARY)

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

.DEFAULT_GOAL := help
