AWS_PROFILE    ?= gdb-delphi-dev
AWS_ACCOUNT_ID ?= 475275892927
AWS_REGION     ?= us-east-1

ECR_REGISTRY        ?= $(AWS_ACCOUNT_ID).dkr.ecr.$(AWS_REGION).amazonaws.com
ECR_REPOSITORY_NAME ?= apollo/ui-test-selinoid-runner
ECR_REGISTRY_URI    ?= $(ECR_REGISTRY)/$(ECR_REPOSITORY_NAME)

REVISION  ?= $(shell git rev-parse --short=8 HEAD)

DEV_IMAGE = ${ECR_REGISTRY}/${ECR_REPOSITORY_NAME}:testing

VENV      = /usr/local
PYTHON    = $(VENV)/bin/python
PIP       = $(VENV)/bin/pip
PYTEST    = $(VENV)/bin/pytest
PYLINT    = $(VENV)/bin/pylint
ISORT     = $(VENV)/bin/isort
MYPY      = $(VENV)/bin/mypy
PIP_AUDIT = $(VENV)/bin/pip-audit
FLAKE     = $(VENV)/bin/flake8
BANDIT    = $(VENV)/bin/bandit
BLACK     = $(VENV)/bin/black

## -
## 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/^/ /'

## -
## Local commands:

##   venv - create local virtual env
.PHONY:venv
venv:
	@echo "Creating virtual environment"
	$(PIP) install -r requirements/base-internal.txt

##   devenv - create local virtual env (dev.txt dependencies)
.PHONY:devenv
devenv: venv
	@echo "Installing dev requirements"
	$(PIP) install -r requirements/dev.txt

##   checkstyle - run checkstyle locally
.PHONY:checkstyle
checkstyle: devenv
	@echo "Run checkstyle"
	$(ISORT) -w 300 --check-only --quiet ./app/
	$(FLAKE) ./app/ --max-line-length=300 --ignore=E203,E501,E741,W503,E712,W605 --max-complexity=36

##   dependencies-check - run safety locally
.PHONY:dependencies-check
dependencies-check: devenv
	@echo "Run dependencies check"
	$(PIP_AUDIT) -r requirements/base.txt

##   dependencies-check - run bandit locally
.PHONY:owasp
owasp: devenv
	@echo "Run owasp check"
	$(BANDIT) -sB101,B311 -x ./app/src/tests/ -r ./app/src/

##   static-typing - run mypy locally
.PHONY:static-typing
static-typing: devenv
	@echo "Run static-typing check"
	$(MYPY) ./app/

##   static-typing - run safety and bandit locally
.PHONY:security
security: dependencies-check owasp

##   prettify - run prettify locally
.PHONY:prettify
prettify: devenv
	$(BLACK) ./app/ --exclude='/(\.eggs|\.git|\.hg|\.mypy_cache|\.nox|\.tox|\.venv|venv|_build|buck-out|build|dist|migrations|.pytest_cache)/' --line-length=300 --target-version=py39
	$(ISORT) -w 300 ./app/

.PHONY:pre-commit
pre-commit: prettify checkstyle security

## -
## Docker commands:


##   docker/login - login to AWS ECR registry using docker, aws configuration need to be set first
.PHONY:docker/login
docker/login:
	@aws ecr get-login \
		--profile ${AWS_PROFILE} \
		--no-include-email \
		--region ${AWS_REGION} | awk '{print $$6}' | \
		docker login -u AWS --password-stdin https://$(ECR_REGISTRY) || aws ecr get-login-password \
			--profile ${AWS_PROFILE} \
			--region ${AWS_REGION} | \
			docker login --username AWS --password-stdin https://$(ECR_REGISTRY)

##   docker/image/build - build lambda package for each environment and put all of them into one docker container
.PHONY: docker/image/build
docker/image/build: docker/login
	@echo "Building docker image"
	docker build \
		--build-arg VERSION=$(VERSION) \
		--build-arg REVISION=$(REVISION) \
		--build-arg BUILDTIME=$(BUILDTIME) \
		--target main \
		-t $(ECR_REGISTRY_URI):master \
		-t $(ECR_REGISTRY_URI):$(REVISION) .
	docker build \
		--build-arg VERSION=$(VERSION) \
		--build-arg REVISION=$(REVISION) \
		--build-arg BUILDTIME=$(BUILDTIME) \
		--target dev \
		-t $(DEV_IMAGE) .

##   docker/image/push - push an image to a registry
.PHONY: docker/image/push
docker/image/push: docker/login
	@echo "Push docker image"
	docker push $(ECR_REGISTRY_URI):master
	docker push $(ECR_REGISTRY_URI):$(REVISION)

##   docker/image/pull - pull an image from a registry
.PHONY: docker/image/pull
docker/image/pull: docker/login
	@echo "Pull docker image"
	docker pull $(ECR_REGISTRY_URI):master

##   docker/<local target> - universal docker wrapper for local targets
docker/%:
	docker run --rm \
		--env-file .env.sample \
		-v $(PWD):/app \
		$(DEV_IMAGE) make $*

.PHONY:clean-up
clean-up:
	del /f /s /q .pytest_cache allure_report automation.log report.html

.PHONY:run-test
run-test:
	py.test --alluredir=allure_report app/src/tests/test_login.py

.PHONY:test
test: clean-up run-test

.PHONY:report
report:
	allure serve allure_report
