.PHONY: pip_dev 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 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

dev: env  # Run dev application
	poetry run python dev.py

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

lint: pip_dev  ## Run code linters
	poetry run mypy monday_com_orca_backend tests
	poetry run ruff check monday_com_orca_backend tests
	poetry run ruff format monday_com_orca_backend tests --diff


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

test_unit: pip_dev  ## Run unit tests
	poetry run pytest tests/unit/ \
		--cov monday_com_orca_backend \
		--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 \
		monday-com-orca-backend-dev

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

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

down_deploy:  # Bring down all docker containers associated with running the deploy service
	docker compose down monday-com-orca-backend-deploy

docker_integration_test:  # Run integration tests in Docker
	docker compose up \
		--build \
		--exit-code-from integration-test \
		--abort-on-container-exit \
		--remove-orphans \
		monday-com-orca-backend-deploy 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
