.PHONY: clean pip_dev dev \
	lint test test_debug test_integration \
	ci_unit_lint ci_test_integration ci_unit_lint_clean

env:
	python3.13 -m venv env
	env/bin/python -m pip install -q -U pip build wheel

env/bin/flask env/bin/py.test env/bin/ruff pip_dev: env
	env/bin/python -m pip install -q -r requirements-dev.txt

dev: | env/bin/flask
	env/bin/python dev.py

lint: | env/bin/ruff
	env/bin/python -m ruff check product_review tests *.py

test: | env/bin/py.test
	env/bin/python -m pytest \
		tests/unit/ tests/functional/ \
		--cov product_review \
		--cov-report term-missing

test_integration: | env/bin/py.test
	env/bin/python -m pytest tests/integration -v

clean:
	git config --unset-all core.hooksPath || true
	rm -rf env .coverage .pytest_cache .cache coverage.xml* pyunit.xml build
	find . | grep -E '__pycache__|\.pyc|\.pyo$\' | xargs rm -rf

pip_lock_versions: clean env
	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

test_debug: | env/bin/py.test
	env/bin/python -m pytest \
		tests/unit tests/functional \
		-s -x -v

ci_unit_lint_clean:
	docker compose down --remove-orphans
	docker rm -f ows-product-review-unit-lint

ci_unit_lint: ci_unit_lint_clean
	docker compose run \
		--rm \
		--name ows-product-review-unit-lint \
		--build \
		lint
	docker compose run \
		--name ows-product-review-unit-lint \
		--build \
		test-unit
	docker cp ows-product-review-unit-lint:/var/app/pyunit.xml ./pyunit.xml
	docker cp ows-product-review-unit-lint:/var/app/coverage.xml ./coverage.xml
# set the coverage report path to the local machine directory so jenkins can find the sources
	sed -i.bak "s?<source>/var/app/product_review</source>?<source>$$(pwd)/product_review</source>?g" coverage.xml
	docker rm -f ows-product-review-unit-lint

ci_test_integration:
	docker compose run \
		--rm \
		--build \
		--remove-orphans \
		test-integration
