.PHONY: pip_dev lint fmt test_unit clean docker_unit_lint
.PHONY: ci_unit_lint_clean ci_unit_lint down_unit_lint down

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

env pip_dev:  ## Prepare environment
	poetry install

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

lint: pip_dev  ## Run code linters
	poetry run mypy src tests --no-namespace-packages
	poetry run ruff check src tests
	poetry run ruff format src tests --diff

fmt:  pip_dev ## Run code formatters
	poetry run ruff check src tests --fix
	poetry run ruff format src tests

test_unit: pip_dev  ## Run unit tests
	poetry run pytest tests/unit/ \
		--cov asset_copy src \
		--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_deploy:   # Bring up dockerized deploy function
	docker compose up \
		--build \
		--detach \
		--remove-orphans \
		function

down_deploy:  # Bring down all docker containers associated with running the deploy function
	docker compose down function

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
