.PHONY: build test test-race test-short lint clean run docker-build docker-up docker-down smoke migrate

BINARY  = taskapi
VERSION ?= dev
IMAGE   = taskapi

build:
	go build -ldflags "-X main.version=$(VERSION)" -o $(BINARY) ./cmd/taskapi

test:
	go test ./...

test-race:
	go test -race -count=1 ./...

test-short:
	go test -short ./...

test-cover:
	go test -coverprofile=coverage.out ./...
	go tool cover -html=coverage.out -o coverage.html

lint:
	go vet ./...

clean:
	rm -f $(BINARY) coverage.out coverage.html

run: build
	. .env && ./$(BINARY)

docker-build:
	docker build --build-arg VERSION=$(VERSION) -t $(IMAGE):$(VERSION) -t $(IMAGE):latest .

docker-up:
	docker-compose up -d

docker-down:
	docker-compose down

docker-logs:
	docker-compose logs -f

smoke: docker-up
	@echo "Waiting for service to be healthy..."
	@sleep 8
	./smoke_test.sh http://localhost:8080 http://localhost:9090

migrate:
	migrate -path migrations -database "$(DATABASE_URL)" up

migrate-down:
	migrate -path migrations -database "$(DATABASE_URL)" down 1

migrate-version:
	migrate -path migrations -database "$(DATABASE_URL)" version

.DEFAULT_GOAL := build
