.PHONY: dev trace-dev lint test test_unit pip_test_integration test_integration clean clean_test_integration docker_unit_lint up_dev down_dev up_deploy down_deploy
.PHONY: ci_unit_lint_clean ci_unit_lint
.PHONY: up down up_dev down_dev up_deploy down_deploy
.PHONY: docker_integration_test down_integration ci_test_integration ci_test_integration_clean

# Used by `docker compose` dev targets that use multiple yml files.
COMPOSE_DEV_FILES = -f docker-compose.yml -f docker-compose-dev.yml

help:
	@fgrep -h "##" $(MAKEFILE_LIST) | fgrep -v fgrep | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}'

env:  ## Prepare environment
	uv sync

clean:  ## Clean environment
	rm -rf env .venv .cache .pytest_cache .ty_cache .ruff_cache pyunit.xml .coverage coverage.xml
	find . | grep -E '__pycache__|\.pyc|\.pyo$\' | xargs rm -rf

dev:  ## Run dev application
	uv run python dev.py

trace-dev:  ## Run dev application with ddtrace
	set -o allexport; source .env; set +o allexport; uv run ddtrace-run python dev.py

lint:  ## Run code linters
	uv run ty check contributor tests
	uv run ruff check contributor tests
	uv run ruff format contributor tests --diff

fmt:  ## Run code formatters
	uv run ruff check contributor tests --fix
	uv run ruff format contributor tests

test_unit:  ## Run unit tests
	uv run pytest tests/unit/ \
		--cov contributor \
		--cov-report xml \
		--cov-report term \
		--junitxml=pyunit.xml

test_integration: ## Run integration tests
	uv run pytest tests/integration/ \
		--cov contributor \
		--cov-report xml \
		--cov-report term \
		--junitxml=pyunit.xml

docker_unit_lint:  ## Run lint and unit tests in Docker
	docker compose up \
		--build \
		--exit-code-from unit-lint \
		--abort-on-container-exit \
		--remove-orphans \
		unit-lint

down_unit_lint:  ## Teardown any remaining Docker containers related to lint and unit tests
	docker compose down unit-lint --remove-orphans

ci_unit_lint: ci_unit_lint_clean  ## Run lint and unit tests in CI/CD
	mkdir -p build
	chmod a+w build
	make docker_unit_lint

ci_unit_lint_clean: down_unit_lint  ## Teardown lint and unit tests in CI/CD
	rm -rf build

up: up_dev  ## Bring up dockerized dev service (supports hot-reload)

down:  ## Bring down all docker containers
	docker compose $(COMPOSE_DEV_FILES) down

up_dev:  ## Bring up dockerized dev service (supports hot-reload)
	docker compose $(COMPOSE_DEV_FILES) up \
		--detach \
		--remove-orphans \
		contributor-dev

down_dev:  ## Bring down all docker containers associated with running the dev service
	docker compose $(COMPOSE_DEV_FILES) down contributor-dev

up_deploy:  ## Bring up dockerized deploy service
	docker compose build \
		--no-cache \
		contributor-deploy
	docker compose $(COMPOSE_DEV_FILES) up \
		--detach \
		--remove-orphans \
		contributor-deploy

down_deploy:  ## Bring down all docker containers associated with running the deploy service
	docker compose down contributor-deploy

docker_integration_test:  ## Run integration tests in Docker
	docker compose up \
		--build \
		--exit-code-from integration-test \
		--abort-on-container-exit \
		--remove-orphans \
		integration-test

down_integration: down  ## Teardown any remaining Docker containers related to integration tests

ci_test_integration: ci_test_integration_clean  ## Run integration tests in CI/CD
	make docker_integration_test

ci_test_integration_clean: down_integration  ## Teardown integration tests in CI/CD
