.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: podcast tests/ dev.py
	env/bin/ruff check --fix podcast/ tests/ scripts/ dev.py dev/rss_ingester.py dev/qa-users-cleanup.py

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

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

test_integration: tests/integration/env tests/integration
	tests/integration/env/bin/py.test tests/integration -vv -n auto --junitxml=pyunit.xml

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-podcast .

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

docker-redeploy:
	# stop all current tasks to force deploy
	aws ecs list-tasks --cluster dev-ows-podcast --output text 2>&1 | awk '{print $$2}' | xargs -L 1 aws ecs stop-task --cluster dev-ows-podcast --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_test_integration: clean_test_integration test_integration

ci_unit_lint: clean pip_dev lint test_unit
