SQL_DATABASE?=./pulley.db
COVER_PROFILE?=./tmp/profile.out

# TODO maybe only use -race in an ARG for CI
FLAGS := -race -v 

# `make <target> ARGS=no-cache` will avoid using cached results in go test.
ifeq ($(ARGS), no-cache)
	FLAGS += -count=1
endif

# make test ARGS=no-cache
test:
	go test $(FLAGS) ./...
.PHONY: test

test-short:
	go test -short $(FLAGS) ./...
.PHONY: test-short

test-int:
	go test $(FLAGS) -count=1 -tags=integration ./cmd/api/...
.PHONY: test-int

## Coverage See also -covermode=count and -covermode=atomic
cover-html: test-cover
	go tool cover -html=$(COVER_PROFILE)
.PHONY:cover-html

coverage: test-cover
	go tool cover -func=$(COVER_PROFILE)
.PHONY: coverage

test-cover:
	go test -coverprofile=$(COVER_PROFILE) $(FLAGS) ./...
.PHONY: test-cover

serve:
	go run ./cmd/api/...
.PHONY: serve

start-local:
	mkdir -p ./.pg_data && \
	chown $$(id -u):$$(id -g) ./.pg_data && \
	UID=$$(id -u) GID=$$(id -g) docker compose up -d
.PHONY: start-local

db-local:
	docker exec -it pulleydb psql postgres://pulley:pulley@localhost:5432/pulley
.PHONY: db-local

stop-local:
	docker compose down
.PHONY: stop-local

# Checks system dependencies needed to run the local dev environment
check-system-deps:
	@echo "Checking system dependencies..."
	@command -v air > /dev/null || (echo "Missing air command. go install github.com/air-verse/air@latest"; exit 1)
	@command -v psql > /dev/null || (echo "Missing psql command. brew install libpq"; exit 1)
	@command -v migrate > /dev/null || (echo "Missing migrate command. go install -tags 'sqlite3' github.com/golang-migrate/migrate/v4/cmd/migrate@latest"; exit 1)
	@echo "System dependencies fulfilled 👍"
.PHONY: check-system-deps
# Certs

local-certs:
	cd ./tls && go run /usr/local/go/src/crypto/tls/generate_cert.go --rsa-bits=2048 --host=localhost
.PHONY: local-certs
