.PHONY: format lint \
        clean_html_report test_integration test_all_integration docker_test_integration \
        ecr_login up_rie_lambda test_rie_lambda down_rie_lambda \
        docker_rie_test

# Root directory of the tests: lambda-moneyhub/tests
ROOT_DIR     := $(shell dirname $(realpath $(firstword $(MAKEFILE_LIST))))
ECR_REGISTRY := 086679231553.dkr.ecr.us-east-1.amazonaws.com

# ── Code quality ──────────────────────────────────────────────────────────────

format:
	ruff check . *.py --fix
	ruff format . *.py

lint:
	ruff check . *.py
	ruff format . *.py --check --diff
	yamllint -s docker-compose.yaml
	mypy integration/ src/ config.py

# ── QA integration tests ──────────────────────────────────────────────────────

clean_html_report:
	rm -rf report && mkdir report

## Run tests for a specific lambda
test_integration:
ifeq ($(function),)
	@echo "❌ Please provide a lambda function name, e.g: make test_integration function='generate_attachments'"
	@exit 1
else
	@echo "▶ Running integration tests for lambda: $(function)"
	pytest $(ROOT_DIR)/integration/$(function)/test*.py -o "addopts=" --log-cli-level=INFO --html=report/report_$(function).html --self-contained-html --reruns 1 --reruns-delay 1
endif

## Run integration tests for all lambdas in parallel via xdist
test_all_integration: clean_html_report
	@echo "▶ Running integration tests for all lambdas in parallel..."
	pytest -n auto --dist=loadfile -o "addopts=" \
		$(ROOT_DIR)/integration/*/test*.py \
		--html=report/report_all.html --self-contained-html \
		--reruns 1 --reruns-delay 1

docker_test_integration:
	function="$(function)" docker compose $(if $(function),--project-name $(function),) run --build --rm lambda-integration-tests

# ── RIE (Runtime Interface Emulator) ─────────────────────────────────────────

ecr_login:
	aws ecr get-login-password --region us-east-1 \
		| docker login --username AWS --password-stdin $(ECR_REGISTRY)

up_rie_lambda:
	@test -n "$(function)" || (echo "❌ Provide function=, e.g: make $@ function='custom_reports'" && exit 1)
	docker compose --profile $(function) --project-name rie-$(function) up --build -d $(subst _,-,$(function))-lambda


test_rie_lambda:
	@test -n "$(function)" || (echo "❌ Provide function=, e.g: make $@ function='custom_reports'" && exit 1)
	LAMBDA_ENDPOINT_URL=http://$(subst _,-,$(function))-lambda:8080 \
	function=$(function) \
	docker compose --project-name rie-$(function) run --rm lambda-integration-tests

down_rie_lambda:
	@test -n "$(function)" || (echo "❌ Provide function=, e.g: make $@ function='custom_reports'" && exit 1)
	docker compose --profile $(function) --project-name rie-$(function) down

## Build RIE lambda and run integration tests against it in a shared Docker network (CI and local)
## On CI pass BUILD_FLAG=--no-build when images are pre-built outside the AWS credentials context
BUILD_FLAG ?= --build
# docker compose run does not support --no-build; map to --build or empty
RUN_BUILD_FLAG = $(if $(filter --build,$(BUILD_FLAG)),--build)

docker_rie_test:
	@test -n "$(function)" || (echo "❌ Provide function=, e.g: make $@ function='custom_reports'" && exit 1)
	docker compose --profile $(function) --project-name rie-$(function) \
		up $(BUILD_FLAG) -d --wait $(subst _,-,$(function))-lambda && \
	LAMBDA_ENDPOINT_URL=http://$(subst _,-,$(function))-lambda:8080 \
	function=$(function) \
	docker compose --project-name rie-$(function) run $(RUN_BUILD_FLAG) --rm lambda-integration-tests; \
	EXIT_CODE=$$?; \
	docker compose --profile $(function) --project-name rie-$(function) \
		down --remove-orphans; \
	exit $$EXIT_CODE
