# Add any tasks that are not dependent on files to the .PHONY list.
.PHONY: install_from_lock install_from_lock_include_dev install_with_dev install_without_dev_from_lock lint test test_unit test_unit_path test_unit_cov clean

# Install all dependencies (including dev). Don not update Pipfile.lock
install_from_lock_include_dev:
	uv sync --locked --dev
	uv pip install -e .

# Install all dependencies (dev is not included). Don not update Pipfile.lock
install_from_lock:
	uv sync --locked --no-dev
	uv pip install .

# Update and install all dependencies (including dev)
install_with_dev:
	uv lock --upgrade
	uv pip install .

uv_lock:
	uv lock

install_without_dev_from_lock:
	uv sync --locked
	uv pip install -e .

lint_code:
	uv run --no-env-file flake8 feed_ingestion/ tests/

check_deps:
	uv run deptry .

check_lock:
	uv lock --check

lint: lint_code check_deps check_lock

test_unit:
	uv run py.test tests/

test: test_unit

# Run tests for single path (file or directory) in tests/
# 	i.e. 'make test_unit_path TEST_PATH=tests/flows/deezer'
test_unit_path:
	uv run py.test $(TEST_PATH) -x -vv

test_unit_cov:
	uv run --no-env-file  py.test \
		--cov=. tests/ \
		--cov-report xml:build/coverage.xml \
		--cov-branch \
		--junitxml=build/pyunit.xml

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

docker_login_dev:
	aws ecr get-login-password --region us-east-1 | docker login --username AWS --password-stdin 103233932089.dkr.ecr.us-east-1.amazonaws.com

docker_build:
	docker compose build  --progress=plain --build-arg version=$$(git rev-parse HEAD)

docker_unit_lint:
	mkdir -p build/
	chmod -R go+rw build/
	docker compose up \
		--build \
		--exit-code-from unit-lint \
		--abort-on-container-exit \
		--remove-orphans \
		unit-lint

docker_scan:
	echo Make sure you have run 'make docker_build' first
	docker save swf-feed-ingestion-unit-lint -o image.tar
	# VULNERABILITIES_TO_IGNORE should be synced with Jenkinsfile
	docker run --rm \
		--pull always \
		-v ./image.tar:/var/app/image.tar \
		-e AWS_ACCESS_KEY_ID \
		-e AWS_SECRET_ACCESS_KEY \
		-e AWS_SESSION_TOKEN \
		-e VULNERABILITIES_TO_IGNORE=CVE-2018-1000613,CVE-2018-1000180,CVE-2023-6378,CVE-2023-6481,CVE-2024-29857,CVE-2022-45688,CVE-2023-5072,CVE-2023-31582,CVE-2017-5929 \
		086679231553.dkr.ecr.us-east-1.amazonaws.com/docker-image-scanner

clean:
	rm *_Reporter.properties

FLOW_NAME ?= $(shell grep -E '^(export )?FLOW_NAME=' .env 2>/dev/null | cut -d= -f2-)

# 	i.e. 'make format FLOW_NAME=amazon_datapulse' or set FLOW_NAME in .env
format_flow:
	@if [ -z "$(FLOW_NAME)" ]; then echo "Error! Set FLOW_NAME in .env or use: make format FLOW_NAME=amazon_datapulse"; exit 1; fi
	ruff format feed_ingestion/flows/$(FLOW_NAME) tests/flows/$(FLOW_NAME)
	ruff check --fix feed_ingestion/flows/$(FLOW_NAME) tests/flows/$(FLOW_NAME)

test_flow:
	@if [ -z "$(FLOW_NAME)" ]; then echo "Error! Set FLOW_NAME in .env or use: make format FLOW_NAME=amazon_datapulse"; exit 1; fi
	uv run py.test tests/flows/$(FLOW_NAME) -x -vv


docker_unit_lint_clean:
	docker compose down --remove-orphans
