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

clean: ## Remove build, test, coverage, and Python artifacts
	# files
	rm -f -- .coverage coverage.xml pyunit.xml report.html
	# dirs
	rm -rf -- .cache .pytest_cache
	# remove __pycache__ dirs and .pyc/.pyo files
	find . \( -path './.venv' -o -path './venv' -o -path './env' \) -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

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 -- python 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 -- python -m pytest tests/unit tests/functional --cov abacus_legacy_sync --cov-report term-missing

start_db:
	docker compose up -d abacus-legacy-sync-mysql-test
	docker compose up -d abacus-legacy-sync-ows-carveouts-python

stop_db:
	docker compose down abacus-legacy-sync-mysql-test --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-legacy-sync-ows-abacus-legacy-sync
	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-legacy-sync-ows-abacus-legacy-sync --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
