.PHONY: clean clean_all env dev_env dev \
	openapi lint lint_fix format format_fix \
	test start_db stop_db ci_unit_lint ci_unit_lint_clean \
	ci_test_integration ci_test_integration_clean docker_unit_lint down_unit_lint docker_test_integration \
	docker_test_integration_local down_test_integration docker_login test_integration_local docker_override

clean: ## Remove build, test, coverage, and Python artifacts
	# files
	rm -f -- .coverage coverage.xml pyunit.xml
	# dirs
	rm -rf -- .cache .pytest_cache
	# remove __pycache__ dirs and .pyc/.pyo files
	find . \( -path './.venv' -o -path './venv' \) -prune \
		-o \( -type d -name __pycache__ -exec rm -rf -- {} + -o -type f -name '*.py[co]' -delete \)

clean_all: clean ## Also remove virtualenv
	rm -rf -- .venv

env: ## Install default dependencies
	uv sync --frozen

dev_env: ## Install default and dev dependencies
	uv sync --group dev --frozen

dev: dev_env ## Run the dev entrypoint
	uv run --no-sync -- dev.py

openapi:
	uv run -m common_apispec.generator


lint: dev_env ## Run the linter to check
	uv run --no-sync -- ruff check

lint_fix: dev_env ## Run the linter to fix
	uv run --no-sync -- ruff check --fix

format: dev_env ## Run the formatter to check
	uv run --no-sync -- ruff format --check --diff

format_fix: dev_env ## Run the formatter to fix
	uv run --no-sync -- ruff format

typecheck: dev_env ## Run the typechecker
	uv run --no-sync -- mypy abacus_state/

test: dev_env ## Run unit/functional tests with coverage
	SQLITE_DIR=./sqlite ./scripts/test.sh

start_db:
	docker compose up -d abacus-state-mysql
	docker compose up abacus-state-liquibase-runner --quiet-pull

stop_db:
	docker compose down abacus-state-mysql --volumes --remove-orphans
	docker compose down abacus-state-liquibase-runner --volumes --remove-orphans
	rm -rf -- ./sqlite/

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

ci_test_integration: ci_test_integration_clean
	mkdir -p build
	chmod a+w build
	make docker_test_integration

ci_test_integration_clean: down_test_integration
	rm -rf build

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

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

docker_test_integration: start_db
	mkdir -p build
	chmod a+w build
	docker compose up -d abacus-state-ows-abacus-state
	docker compose up \
		--abort-on-container-exit \
		--build \
		--exit-code-from test-integration \
		--quiet-pull \
		--remove-orphans \
		test-integration

docker_test_integration_local: start_db
	mkdir -p build
	chmod a+w build
	docker compose up -d abacus-state-local
	docker compose up \
		--abort-on-container-exit \
		--build \
		--exit-code-from test-integration-local \
		--quiet-pull \
		--remove-orphans \
		test-integration-local

down_test_integration: stop_db
	docker compose down abacus-state-ows-abacus-state --volumes --remove-orphans
	docker compose down abacus-state-local --volumes --remove-orphans
	docker compose down test-integration --volumes --remove-orphans
	docker compose down test-integration-local --volumes --remove-orphans

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

test_integration_local: ci_test_integration_clean
	mkdir -p build
	chmod a+w build
	make docker_test_integration_local

docker_override:
	if [ ! -f docker-compose.override.yml ]; then cp docker-compose.override.yml.shadow docker-compose.override.yml; fi
