.PHONY: ci_test_integration ci_test_integration_clean \
	ci_unit_lint ci_unit_lint_clean \
	clean clean_all \
	dev_env \
	docker_test_integration docker_test_integration_down \
	docker_unit_lint docker_unit_lint_down \
	env env_integration \
	help \
	lint lint_fix \
	requirements \
	test test_integration test_unit

help:
	@echo "=========================="
	@echo "python-abacus-common-logic"
	@echo "=========================="
	@echo ""
	@echo "Development targets:"
	@echo "  env                    - Install production dependencies"
	@echo "  dev_env                - Install development dependencies"
	@echo "  env_integration        - Install integration test dependencies"
	@echo ""
	@echo "Code quality:"
	@echo "  lint                   - Run linter"
	@echo "  lint_fix               - Fix linting issues automatically"
	@echo "  format                 - Check code formatting (ruff format)"
	@echo "  format_fix             - Fix code formatting automatically"
	@echo ""
	@echo "Testing:"
	@echo "  test                   - Run all tests with coverage"
	@echo "  test_unit              - Run unit tests with coverage report"
	@echo "  test_integration       - Run integration tests"
	@echo ""
	@echo "Build:"
	@echo "  requirements           - Generate requirements.txt and requirements-dev.txt"
	@echo ""
	@echo "Cleanup:"
	@echo "  clean                  - Remove cache files and build artifacts"
	@echo "  clean_all              - Remove cache files and virtual environment"
	@echo ""
	@echo "CI/CD:"
	@echo "  ci_unit_lint           - Run unit lint in Docker"
	@echo "  ci_unit_lint_clean     - Clean up unit lint Docker resources"
	@echo "  ci_test_integration    - Run integration tests in Docker"
	@echo "  ci_test_integration_clean - Clean up integration Docker resources"
	@echo ""
	@echo "Docker:"
	@echo "  docker_unit_lint       - Run unit tests and linting in Docker"
	@echo "  docker_unit_lint_down  - Stop and remove unit lint Docker containers"
	@echo "  docker_test_integration - Run integration tests in Docker"
	@echo "  docker_test_integration_down - Stop and remove integration Docker containers"

clean:
	@echo "Cleaning up..."
	find . -type d -name "__pycache__" -exec rm -rf {} + 2>/dev/null || true
	find . -type d -name ".pytest_cache" -exec rm -rf {} + 2>/dev/null || true
	find . -type d -name ".ruff_cache" -exec rm -rf {} + 2>/dev/null || true
	find . -type f -name "*.pyc" -delete 2>/dev/null || true
	find . -type f -name "*.pyo" -delete 2>/dev/null || true
	rm -rf -- build dist *.egg-info .cache .coverage \
		coverage.xml pyunit.xml
	@echo "Done!"

clean_all: clean
	@echo "Removing virtual environment..."
	rm -rf .venv
	@echo "All clean!"

dev_env:
	@echo "Installing dev dependencies..."
	uv sync --group dev --frozen

env:
	@echo "Installing dependencies..."
	uv sync --frozen

env_integration:
	@echo "Installing integration dependencies..."
	uv sync --group integration --frozen

format: dev_env
	@echo "Checking formatting..."
	uv run --no-sync -- ruff format --check --diff

format_fix: dev_env
	@echo "Fixing formatting..."
	uv run --no-sync -- ruff format

lint: dev_env
	@echo "Running linter..."
	uv run --no-sync -- ruff check

lint_fix: dev_env
	@echo "Fixing linting..."
	uv run --no-sync -- ruff check --fix

requirements: clean_all requirements_default requirements_dev env_integration

requirements_default:
	@echo "Creating requirements.txt..."
	uv sync --no-dev --no-install-project --frozen
	echo "-i https://pypi.theorchard.io/pypi/" > requirements.txt
	uv pip freeze >> requirements.txt

requirements_dev:
	@echo "Creating requirements-dev.txt..."
	uv sync --group dev --no-install-project --frozen
	echo "-i https://pypi.theorchard.io/pypi/" > requirements-dev.txt
	uv pip freeze >> requirements-dev.txt


#############
### Tests ###
#############

test: env_integration
	uv run --no-sync -- python -m pytest \
		tests/unit tests/functional tests/integration \
		--cov abacus_common_logic \
		--cov-report term-missing \
		-s

test_integration: env_integration
	uv run --no-sync -- python -m pytest \
		tests/integration

test_unit: dev_env
	uv run --no-sync -- python -m pytest \
		tests/unit tests/functional \
	    --cov abacus_common_logic \
		--cov-report xml \
		--junitxml=build/pyunit.xml

#############
### CI/CD ###
#############

ci_test_integration: ci_test_integration_clean docker_test_integration

ci_test_integration_clean: docker_test_integration_down

ci_unit_lint: ci_unit_lint_clean
	mkdir -p build
	chmod a+w build
	make docker_unit_lint

ci_unit_lint_clean: docker_unit_lint_down
	rm -rf build

##############
### Docker ###
##############

docker_login:
	aws ecr get-login-password --region us-east-1 | docker login --username AWS --password-stdin 086679231553.dkr.ecr.us-east-1.amazonaws.com

docker_test_integration:
	docker compose up \
		--abort-on-container-exit \
		--build \
		--exit-code-from test-integration \
		--remove-orphans \
		test-integration

docker_test_integration_down: 
	docker compose down test-integration --remove-orphans --volumes

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

docker_unit_lint_down:
	docker compose down unit-lint --remove-orphans --volumes
