.PHONY: help clean env pip_dev test_unit test_cov fmt lint docker-build docker-test docker-lint ci_unit_lint ci_unit_lint_clean

help:
	@fgrep -h "##" $(MAKEFILE_LIST) | fgrep -v fgrep | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}'

clean:  ## Clean environment
	rm -rf env .cache .pytest_cache .mypy_cache .ruff_cache pyunit.xml .coverage coverage.xml
	find . | grep -E '__pycache__|\.pyc|\.pyo$\' | xargs rm -rf

env pip_dev:  ## Prepare environment
	uv sync --group dev

test_unit: pip_dev  ## Run unit tests
	uv run pytest tests/unit/ \
		--cov sentry_scrubber \
		--cov-report xml \
		--cov-report term \
		--junitxml=pyunit.xml

test_cov: pip_dev  ## Run unit tests with coverage
	uv run pytest tests/ \
		--cov sentry_scrubber \
		--cov-report term-missing

lint: pip_dev  ## Run code linters
	uv run mypy sentry_scrubber tests
	uv run ruff check sentry_scrubber tests
	uv run ruff format sentry_scrubber tests --check --diff

fmt:  pip_dev ## Run code formatters
	uv run ruff check sentry_scrubber tests --fix
	uv run ruff format sentry_scrubber tests

docker-build: ## Build Docker test image
	uv export --group dev --no-hashes > requirements-dev.txt
	docker compose build lint-and-test

docker-test: ## Run tests in Docker
	uv export --group dev --no-hashes > requirements-dev.txt
	docker compose run --rm --build -T lint-and-test

docker-lint: docker-test ## Alias for docker-test (runs linting and tests)

ci_unit_lint:  ## CI: Run unit tests and linting
	docker compose run \
		--build \
		--name python-sentry-scrubber-unit-lint \
		lint-and-test
	docker cp python-sentry-scrubber-unit-lint:/var/task/pyunit.xml ./pyunit.xml
	docker cp python-sentry-scrubber-unit-lint:/var/task/coverage.xml ./coverage.xml
	sed -i.bak "s?<source>/var/task/sentry_scrubber</source>?<source>$$(pwd)/sentry_scrubber</source>?g" coverage.xml
	docker rm -f python-sentry-scrubber-unit-lint



ci_unit_lint_clean:  ## CI: Clean up test artifacts
	docker rm -f python-sentry-scrubber-unit-lint
	docker compose down --remove-orphans
