APP = daemon-notifications-delivery
MAIN_IMAGE_NAME = $(APP)-main
TEST_IMAGE_NAME = $(APP)-tests

AWS_REGION = us-east-1
ECR_ACCOUNT_ID = 086679231553

VENV := ./venv

PYTHON    = $(VENV)/bin/python
PIP       = $(VENV)/bin/pip
PYTEST    = $(VENV)/bin/pytest
FLAKE     = $(VENV)/bin/flake8
ISORT     = $(VENV)/bin/isort
PYBABEL   = $(VENV)/bin/pybabel

.DEFAULT_GOAL := help

## -
## help - this message
.PHONY: help
help: Makefile
	@echo "Application: $(APP)\n"
	@echo "Run command:\n  make <target>\n"
	@grep -E -h '^## .*' $(MAKEFILE_LIST) | sed -n 's/^##//p'  | column -t -s '-' |  sed -e 's/^/ /'

$(VENV)/bin/activate:
	@echo "Creating virtual environment"
	python -m venv $(VENV)/

venv: $(VENV)/bin/activate
	@echo "Update pip"
	$(PIP) install --no-cache-dir -U pip
	@echo "Installing requirements"
	$(PIP) install --no-cache-dir -r requirements.txt

testenv: venv
	@echo "Installing dev requirements"
	$(PIP) install --no-cache-dir -r requirements-dev.txt

## test - run unit tests and create report
.PHONY: test
test: testenv
	@echo "Running unit tests"
	$(PYTEST) tests/unit \
		--cov notifications_delivery/ \
		--cov-report xml:coverage.xml \
		--junitxml=pyunit.xml

## test_integration - run integration tests
.PHONY: test_integration
test_integration: testenv
	@echo "Running integration tests"
	$(PYTEST) -v tests/integration

## lint - check codestyle
.PHONY: checkstyle
checkstyle: testenv
	@echo "Checking codestyle"
	$(ISORT) --check-only --quiet  notifications_delivery/ tests/
	$(FLAKE) notifications_delivery/ tests/

## clean - remove virtual environment
.PHONY: clean
clean:
	@echo "Removing virtual environment"
	rm -rf $(VENV)

## i18n - update translations
.PHONY: i18n
i18n: venv
	./i18n.sh $(PYBABEL)

## dev - run local server
.PHONY: dev
dev:
	./run_local.sh

## dev/datadog - run local server with datadog support
.PHONY: dev/datadog
dev/datadog:
	./run_local.sh datadog

## ci_check - run check suite
.PHONY: ci_check
ci_check: checkstyle test

## pre-commit - run check suite
.PHONY: pre-commit
pre-commit: docker/build/test docker/checkstyle docker/test

## docker/login/base - login to ECR
.PHONY: docker/login
docker/login:
	@aws ecr get-login-password --region $(AWS_REGION) | docker login --username AWS --password-stdin $(ECR_ACCOUNT_ID).dkr.ecr.$(AWS_REGION).amazonaws.com

## docker/build - build main image
.PHONY: docker/build
docker/build:
	@docker build \
		--target main \
		--build-arg POEDITOR_PROJECT_ID=$(POEDITOR_PROJECT_ID) \
		--secret id=poeditor_api_token,env=POEDITOR_API_KEY \
		--tag $(MAIN_IMAGE_NAME) .

## docker/build/test - build test image
.PHONY: docker/build/test
docker/build/test:
	@docker build \
		--target tests \
		--tag $(TEST_IMAGE_NAME) .

## docker/clean
.PHONY: docker/clean
docker/clean:
	@echo "Nothing to clean inside docker, stopping containers if running"
	@docker volume prune -f

## docker/<local target> - universal docker wrapper for local targets
docker/%:
	docker run --rm \
		-v $(PWD):/var/app \
		$(TEST_IMAGE_NAME) make VENV=/var/venv $*
