.PHONY: clean env dev_env pip_lock_versions run lint test \
	docker_local_up docker_local_down local_event \
	docker_test docker_test_up docker_test_down \
	test_integration docker_test_integration_up docker_test_integration_down \
	docker_ows_up docker_ows_down \
	test_integration docker_down

clean:
	if [ -f report.html ]; then rm -r report.html; fi
	rm -rf env .coverage .pytest_cache .cache coverage.xml pyunit.xml
	find . | grep -E '__pycache__|\.pyc|\.pyo$\' | xargs rm -rf

dev_env: env
	if [ ! -f env/bin/py.test ]; then env/bin/python -m pip install -q -r requirements.txt -r requirements-dev.txt; fi

env:
	python3.13 -m venv env
	env/bin/python -m pip install -q -U pip setuptools wheel
	env/bin/python -m pip install -q -I -r requirements.txt

pip_lock_versions:
	python3.13 -m venv env
	env/bin/python -m pip install -q -U pip setuptools wheel
	env/bin/python -m pip install -q -I -r requirements-to-freeze.txt
	echo "-i https://pypi.theorchard.io/pypi/" > requirements.txt
	env/bin/python -m pip freeze >> requirements.txt

run: dev_env
	env/bin/python dev.py

start_db:
	docker compose up -d commit-mechanicals-mysql
	docker compose up commit-mechanicals-liquibase-runner

lint: dev_env
	env/bin/python -m flake8 \
		app.py \
		commit_mechanicals \
		tests/

test: dev_env
	env/bin/python -m pytest tests/unit \
		--ignore=tests/integration \
		--cov app \
		--cov commit_mechanicals \
		--cov-report term-missing

docker_local_up:
	docker compose up --build -d function

docker_local_down:
	docker compose down function --volumes --remove-orphans

local_event:
	curl -XPOST "http://localhost:9000/2015-03-31/functions/function/invocations" \
		-d '{"abacus_event_id": 1, "target_type": "accounting_run", "target_id": "369", "event_name": "commit_mechanicals"}'

docker_test: docker_test_down docker_test_up

docker_test_up:
	docker compose up \
		--exit-code-from lint-and-test \
		--abort-on-container-exit \
		--build lint-and-test

docker_test_down:
	docker compose down lint-and-test --volumes --remove-orphans

docker_test_integration: docker_test_integration_down docker_test_integration_up

docker_test_integration_up: docker_ows_up
	docker compose up -d commit-mechanicals-lambda
	docker compose up \
		--exit-code-from integration-test \
		--abort-on-container-exit \
		--build integration-test

docker_test_integration_down: docker_ows_down
	docker compose down commit-mechanicals-lambda --volumes --remove-orphans
	docker compose down integration-test --volumes --remove-orphans

docker_ows_up:
	docker compose up -d commit-mechanicals-mysql
	docker compose up commit-mechanicals-liquibase-runner
	docker compose up -d commit-mechanicals-ows-ledger
	docker compose up -d commit-mechanicals-lambda

docker_ows_down:
	docker compose down commit-mechanicals-mysql --volumes --remove-orphans
	docker compose down commit-mechanicals-liquibase-runner --volumes --remove-orphans
	docker compose down commit-mechanicals-ows-ledger --volumes --remove-orphans

# REVIEW: We're keeping those commands for now since they're used by CI.
# We might remove them once we migrate to the Jenkinsfile
test_integration: docker_ows_down sync_docker docker_ows_up
	docker compose up \
		--exit-code-from integration-test \
		--abort-on-container-exit \
		--build integration-test

sync_docker:
	docker compose pull

docker_down: docker_test_down docker_test_integration_down
