.PHONY: clean dev_env run \
	test test_unit lint test_integration \
	pip_lock_versions \
	unit_lint_job \
	sync_docker docker_down \
	local_listener local_event

clean:
	if [ -f report.html ]; then rm -r report.html; fi
	rm -rf env .coverage .pytest_cache .cache coverage.xml pyunit.xml
	find . | grep -E '__pycache__|\.pyc|\.pyo$\' | xargs rm -rf

dev_env: env
	if [ ! -f env/bin/py.test ]; then env/bin/python -m pip install -q -r requirements.txt -r requirements-dev.txt; fi

env:
	python3.12 -m venv env
	env/bin/python -m pip install -U pip setuptools wheel
	env/bin/python -m pip install -I -r requirements.txt

lint: dev_env
	env/bin/python -m flake8 \
		app.py \
		schedule_auto_add \
		tests/

pip_lock_versions:
	python3.12 -m venv env
	env/bin/python -m pip install -q -U pip setuptools wheel
	env/bin/python -m pip install -q -I -r requirements-to-freeze.txt
	echo "-i https://pypi.theorchard.io/pypi/" > requirements.txt
	env/bin/python -m pip freeze >> requirements.txt

docker_down:
	docker compose down --remove-orphans

test: dev_env
	env/bin/python -m pytest tests/unit \
		--cov app \
		--cov schedule_auto_add \
		--cov-report term-missing

local_event:
	curl -XPOST "http://localhost:9000/2015-03-31/functions/function/invocations" \
		-d '@tests/sample_event.json'

test_unit: dev_env
	env/bin/python -m pytest tests/unit \
		--ignore=tests/integration \
		--cov app \
		--cov schedule_auto_add \
		--cov-report xml \
		--junitxml=pyunit.xml

unit_lint_job: clean lint test_unit

docker_test:
	docker compose up \
		--exit-code-from lint-and-test \
		--abort-on-container-exit \
		--build lint-and-test

test_integration: start_ows
	docker compose up \
		--exit-code-from integration-test \
		--abort-on-container-exit \
		--build integration-test

docker_local_up:
	docker compose up --build -d function

sync_docker:
	docker compose pull

ows_login:
	aws ecr get-login-password --region us-east-1 | docker login --username AWS --password-stdin 437795906767.dkr.ecr.us-east-1.amazonaws.com && \
	aws ecr get-login-password --region us-east-1 | docker login --username AWS --password-stdin 086679231553.dkr.ecr.us-east-1.amazonaws.com

start_ows: ows_login docker_down sync_docker
	docker compose up -d schedule-auto-add-lambda
