.PHONY: dev dev_env clean \
	start_db local_start_db docker_down \
	test test_unit test_integration format lint \
	drop_integration_db drop_unit_db \
	ci_unit_lint ci_unit_lint_clean \
	ci_test_integration_clean ci_test_integration \
	integration_stack local_integration_stack \
	docker_test spec

clean:
	rm -rf .venv \
		.coverage .pytest_cache \
		.cache \
		build
	find . | grep -E '__pycache__|\.pyc|\.pyo$\' | xargs rm -rf

env:
	uv sync --frozen --no-default-groups

dev_env: env
	uv sync --group dev --frozen

dev: dev_env
	uv run --no-sync dev.py

openapi: dev_env
	uv run --no-sync python -m common_apispec.generator

docker_down:
	docker compose down --remove-orphans

integration_stack:
	docker compose up --build -d ows-payment
	docker compose up -d payment-ows-abacus-state
	docker compose up -d payment-ows-abacus-account

local_integration_stack:
	docker compose -f docker-compose.yml -f docker-compose.local.yml up --build -d ows-payment
	docker compose -f docker-compose.yml -f docker-compose.local.yml up -d payment-ows-abacus-state
	docker compose -f docker-compose.yml -f docker-compose.local.yml up -d payment-ows-abacus-account

format: dev_env
	uv run --no-sync ruff check payment tests *.py --exclude="tests/integration/env/*" --fix
	uv run --no-sync ruff format payment tests *.py --exclude="tests/integration/env/*"

lint: dev_env
	uv run --no-sync ruff check payment tests application.py dev.py --exclude="tests/integration/env/*"
	uv run --no-sync ruff format payment tests application.py dev.py --exclude="tests/integration/env/*" --check --diff

start_db:
	docker compose up -d payment-mysql
	docker compose up payment-liquibase-runner

local_start_db:
	docker compose -f docker-compose.yml -f docker-compose.local.yml up -d payment-mysql
	docker compose -f docker-compose.yml -f docker-compose.local.yml up payment-liquibase-runner

drop_integration_db:
	docker volume rm -f ows-payment_dbdata
	docker volume rm -f ows-payment-integration-tests_dbdata

drop_unit_db:
	docker volume rm -f ows-payment_dbdata
	# Prevent PR and Unit-lint jobs from err'ing when attempting to delete
	# a db that's in use in a concurrent job.
	docker volume rm -f ows-payment-pull-request_dbdata || true
	docker volume rm -f ows-payment-unit-lint_dbdata || true

test: dev_env
	uv run --no-sync pytest tests/unit tests/functional \
		--cov payment \
		--cov-report term-missing

test_integration: dev_env
	mkdir -p build
	uv run --no-sync pytest tests/integration -s \
		  --html=build/report_integration.html \
		  --jira \
		  -s \
		  --self-contained-html \
		  -vv

test_unit: dev_env
	uv run --no-sync pytest \
		--ignore=tests/integration \
		--cov payment tests/ \
		--cov-report xml \
		--junitxml=pyunit.xml

ci_unit_lint_clean:
	docker compose down --remove-orphans || true
	rm -rf build
	make drop_unit_db

ci_unit_lint: start_db
	mkdir -p build && chmod a+w build
	docker compose up \
		--abort-on-container-exit \
		--build \
		--exit-code-from unit-lint \
		--quiet-pull \
		--remove-orphans \
		unit-lint

ci_test_integration_clean:
	docker compose down --remove-orphans || true
	rm -rf build
	make drop_integration_db

ci_test_integration: start_db integration_stack
	mkdir -p build && chmod a+w build
	docker compose up \
		--abort-on-container-exit \
		--build \
		--exit-code-from test-integration \
		--quiet-pull \
		--remove-orphans \
		test-integration

docker_test: ci_unit_lint
	make ci_unit_lint_clean
	make ci_test_integration
	make ci_test_integration_clean

docker_login:
	aws ecr get-login-password --region us-east-1 | docker login --username AWS --password-stdin 437795906767.dkr.ecr.us-east-1.amazonaws.com
	aws ecr get-login-password --region us-east-1 | docker login --username AWS --password-stdin 086679231553.dkr.ecr.us-east-1.amazonaws.com
