# Add any tasks that are not dependent on files to the .PHONY list.
.PHONY: install_from_dev install_with_dev_from_lock run lint test_unit test_unit_cov test_integration doocker/login/base docker/up-redis docker/down

BASE_AWS_REGION     = us-east-1
BASE_AWS_ACCOUNT_ID = 437795906767
BASE_ECR_REGISTRY   = $(BASE_AWS_ACCOUNT_ID).dkr.ecr.$(BASE_AWS_REGION).amazonaws.com

# Update and install all dependencies (including dev)
install_with_dev:
	pipenv --rm install --dev

# Install all dependencies (including dev) from lock file
install_with_dev_from_lock:
	pipenv --rm install --dev --ignore-pipfile

# Run application
run:
	pipenv run python dev.py

lint:
	pipenv run flake8 socials/ tests/
	pipenv run sqlfluff lint --ignore parsing

test_unit:
	pipenv run py.test tests/unit/

test_unit_cov:
	pipenv run py.test \
		--cov socials tests/unit/ \
		--cov-report xml \
		--junitxml=pyunit.xml

test_unit_cov_dockerized:
	pipenv run py.test \
		--cov socials tests/unit/ \
		--cov-report=xml:/var/app/reports/coverage.xml  \
		--junitxml=/var/app/reports/pyunit.xml

test_integration:
	pipenv run py.test tests/integration

test_integration_dockerized:
	pipenv run py.test tests/integration

docker/login/base:
	@aws ecr get-login-password | \
			docker login --username AWS --password-stdin $(BASE_ECR_REGISTRY)

docker/up-redis: docker/login/base
	docker-compose up -d redis

docker/down:
	docker-compose down

docker_unit_lint:  # Run lint and unit tests in Docker
	docker compose up \
		--build \
		--exit-code-from lint-and-test \
		--abort-on-container-exit \
		--remove-orphans \
		  lint-and-test

down_unit_lint:  # Teardown any remaining Docker containers related to lint and unit tests
	docker compose down lint-and-test --remove-orphans

ci_unit_lint: ci_unit_lint_clean  # Run lint and unit tests in CI/CD
	mkdir -p reports
	chmod a+w reports
	make docker_unit_lint

ci_unit_lint_clean: down_unit_lint  # Teardown lint and unit tests in CI/CD
	rm -rf reports
