# Add any tasks that are not dependent on files to the .PHONY list.
.PHONY: clean clean_all env dev_env dev \
	lint lint_fix format format_fix test \
	test_functional test_unit start_db stop_db create_split_file \
	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 \
	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

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 (typed modules per pyproject `files`; non-blocking)
	uv run --no-sync -- mypy

openapi:
	uv run -m common_apispec.generator

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

test_functional: dev_env ## Run functional tests with coverage
	uv run --no-sync -- pytest \
		--cov-report term-missing \
		abacus_contract/tests/functional \
		abacus_file_upload/tests/functional \
		royalties/tests/functional

test_unit: dev_env ## Run unit tests with coverage
	uv run --no-sync -- pytest \
		--cov-report term-missing \
		abacus_contract/tests/unit \
		abacus_file_upload/tests/unit \
		royalties/tests/unit

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

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

create_split_file:
	if [ -d .split ]; then rm -rf .split; fi; \
	if [ ! -f .split ]; then cp .split.shadow .split; fi

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 docker_test_integration

ci_test_integration_clean: down_test_integration
	rm -rf build

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

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

docker_test_integration: start_db create_split_file
	docker compose up -d royalties-ows-royalties
	docker compose up \
		--build \
		--exit-code-from test-integration \
		--abort-on-container-exit \
		--remove-orphans \
		test-integration

docker_test_integration_local: start_db create_split_file
	docker compose up -d royalties-local
	docker compose up \
		--build \
		--exit-code-from test-integration-local \
		--abort-on-container-exit \
		--remove-orphans \
		test-integration-local

down_test_integration: stop_db
	docker compose down royalties-ows-royalties --volumes --remove-orphans
	docker compose down royalties-ows-abacus-account --volumes --remove-orphans
	docker compose down royalties-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

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