# Add any tasks that are not dependent on files to the .PHONY list.
.PHONY: dev pip_dev lint test test_unit
.PHONY: docker_unit_lint down_unit_lint ci_unit_lint ci_unit_lint_clean
.PHONY: up_dev down_dev up_deploy down_deploy
.PHONY: up_integration down_integration ci_test_integration ci_test_integration_clean
.PHONE: down local_test_integration

clean:
	rm -rf venv

dev:
	venv/bin/python dev.py

venv pip_dev:
	python3.11 -m venv venv
	venv/bin/pip install --upgrade pip
	venv/bin/pip install -r requirements.txt
	venv/bin/pip install -r requirements-dev.txt

lint: pip_dev
	venv/bin/ruff check features dev.py application.py tests
	venv/bin/ruff format features dev.py application.py tests --check --diff

fmt:  pip_dev
	venv/bin/ruff check features dev.py application.py tests --fix
	venv/bin/ruff format features dev.py application.py tests

test: venv
	venv/bin/py.test --ignore=tests/integration tests/

test_unit:
	venv/bin/py.test \
		--ignore=tests/integration \
		--cov features tests/ \
		--cov-report xml \
		--junitxml=pyunit.xml

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

up_dev:
	docker compose $(COMPOSE_DEV_FILES) up \
		--detach \
		--remove-orphans \
		ows-features-dev

down_dev:
	docker compose $(COMPOSE_DEV_FILES) down ows-features-dev

up_deploy:
	docker compose build \
		--no-cache \
		ows-features-deploy
	docker compose $(COMPOSE_DEV_FILES) up \
		--detach \
		--remove-orphans \
		ows-features-deploy

down_deploy:
	docker compose down ows-features-deploy

docker_unit_lint:
	docker compose up \
		--build \
		--exit-code-from unit-lint \
		--abort-on-container-exit \
		--remove-orphans \
		unit-lint

down_unit_lint:
	docker compose down unit-lint --remove-orphans

ci_unit_lint: ci_unit_lint_clean
	mkdir -p build
	chmod a+w build
	make docker_unit_lint

ci_unit_lint_clean: down_unit_lint
	rm -rf build

down_integration:
	docker compose down
	docker compose rm -f

up_integration: down_integration
	docker compose pull
	docker compose up \
		--detach \
		int-ows-features

ci_test_integration: down_integration up_integration
	docker compose up \
		--build \
		--exit-code-from test-integration \
		test-integration

ci_test_integration_clean: down_integration

down: ## Shutdown docker app
	docker compose $(COMPOSE_DEV_FILES) down

local_test_integration: down
	docker compose $(COMPOSE_DEV_FILES) build
	docker compose $(COMPOSE_DEV_FILES) up \
		--exit-code-from test-integration \
		--abort-on-container-exit \
		--remove-orphans \
		test-integration
