.PHONY: dev dc pip_dev lint test test_unit test_integration pip_test_integration docker-pipeline docker-build docker-push docker-redeploy clean clean_test_integration

dev: env dev.py
	env/bin/python3.11 dev.py

dc: env dev.py
	docker-compose up --build

env pip_dev: requirements.txt requirements-dev.txt
	python3.11 -m venv env
	env/bin/pip3.11 install --upgrade pip
	env/bin/pip3.11 install -r requirements.txt
	env/bin/pip3.11 install -r requirements-dev.txt

lint: asset_transcoder tests/ dev.py
	env/bin/flake8 asset_transcoder/ tests/ dev.py

test: env tests/unit/
	env/bin/py.test tests/unit/

test_unit: env asset_transcoder tests/unit/
	env/bin/py.test \
		--cov asset_transcoder tests/unit/ \
		--cov-report xml \
		--junitxml=pyunit.xml

test-integration-local: tests/integration/env tests/integration
	ENV=local tests/integration/env/bin/py.test tests/integration -vv -n 2 --junitxml=pyunit.xml --maxfail=1

test-integration-dev: tests/integration/env tests/integration
	ENV=dev tests/integration/env/bin/py.test tests/integration -vv -n 3 --junitxml=pyunit.xml --maxfail=1

test-integration-qa: tests/integration/env tests/integration
	ENV=qa tests/integration/env/bin/py.test tests/integration -vv -n 1 --junitxml=pyunit.xml --maxfail=1

test-integration-prod: tests/integration/env tests/integration
	ENV=prod tests/integration/env/bin/py.test tests/integration -vv -n 2 --junitxml=pyunit.xml --maxfail=1

tests/integration/env pip_test_integration: tests/integration/requirements.txt
	python3.11 -m venv tests/integration/env
	tests/integration/env/bin/pip3.11 install --upgrade pip
	tests/integration/env/bin/pip3.11 install -r tests/integration/requirements.txt

docker-login:
    # perform docker login to AWS ECR. Do it once per session. After that you can run docker-push
	`aws ecr get-login --no-include-email`

docker-build:
	docker build --tag ows-asset-transcoder .

docker-push:
	# push the latest version of the image to AWS ECR on DEV account
	docker tag ows-asset-transcoder 103233932089.dkr.ecr.us-east-1.amazonaws.com/ows-asset-transcoder
	# NOTE: do not forget to run make docker-login before first run
	docker push 103233932089.dkr.ecr.us-east-1.amazonaws.com/ows-asset-transcoder

docker-redeploy:
	# stop all current tasks to force re-deploy
	aws ecs list-tasks --cluster dev-ows-asset-transcoder --output text 2>&1 | awk '{print $$2}' | AWS_PAGER='' xargs -L 1 aws ecs stop-task --cluster dev-ows-asset-transcoder --task

docker-pipeline: docker-build docker-push docker-fargate-deploy

docker-fargate-deploy:
# deploy the latest image to fargate on Dev account
	cd dev; ./fargate-deploy.sh

clean:
	rm -rf env

clean_test_integration:
	rm -rf tests/integration/env

ci_unit_lint: clean pip_dev lint test_unit

ci_test_integration: test-integration-qa
