.PHONY: clean clean_all env dev_env dev lint 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 \
	docker_login

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 --no-default-groups

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

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

test: dev_env ## Run unit/functional tests with coverage
	uv run --no-sync -- pytest tests/unit tests/functional --cov abacus_worksheet --cov-report term-missing

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

stop_db:
	docker compose down abacus-worksheet-mysql --volumes --remove-orphans
	docker compose down abacus-worksheet-liquibase-runner --volumes --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

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
	docker compose up -d abacus-worksheet-ows-abacus-worksheet
	docker compose up \
		--abort-on-container-exit \
		--build \
		--exit-code-from test-integration \
		--quiet-pull \
		--remove-orphans \
		test-integration

down_test_integration: stop_db
	docker compose down abacus-worksheet-ows-abacus-worksheet --volumes --remove-orphans
	docker compose down test-integration --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
