# Variables
PROJECT_NAME := earnings-transfer
DOCKER_COMPOSE := docker compose --project-name $(PROJECT_NAME)

.PHONY: clean clean_all env env_dev lint lint_fix format format_fix test test_e2e \
	docker_local_up docker_local_down local_event \
	docker_test docker_test_up docker_test_down \
	test_integration docker_test_integration_up docker_test_integration_down \
	docker_login docker_down uat analyze

clean:
	@echo "Cleaning up..."
	@find . -type d -name "__pycache__" -exec rm -rf {} + 2>/dev/null || true
	@find . -type d -name ".pytest_cache" -exec rm -rf {} + 2>/dev/null || true
	@find . -type d -name ".ruff_cache" -exec rm -rf {} + 2>/dev/null || true
	@find . -type d -name ".mypy_cache" -exec rm -rf {} + 2>/dev/null || true
	@find . -type f -name "*.pyc" -delete 2>/dev/null || true
	@find . -type f -name "*.pyo" -delete 2>/dev/null || true
	@rm -rf -- build dist *.egg-info .cache .coverage \
		coverage.xml pyunit.xml
	@echo "Done!"

clean_all: clean
	@echo "Removing virtual environment..."
	@rm -rf .venv
	@echo "Done!"

env: ## Install default dependencies
	@uv sync --frozen

env_dev: ## Install default and dev dependencies
	@uv sync --group dev --frozen

env_integration:
	@uv sync --group integration --frozen

format: env_dev ## Run the formatter to check
	@echo "Checking formatting..."
	@uv run --no-sync -- ruff format --check --diff

format_fix: env_dev ## Run the formatter to fix
	@echo "Fixing formatting..."
	@uv run --no-sync -- ruff format

lint: env_dev ## Run the linter to check
	@echo "Running linter..."
	@uv run --no-sync -- ruff check

lint_fix: env_dev ## Run the linter to fix
	@echo "Fixing linting..."
	@uv run --no-sync -- ruff check --fix

###############
### Testing ###
###############

run: env_dev
	uv run -- python dev.py

test: env_dev ## Run unit tests with coverage
	uv run --no-sync -- pytest tests/unit \
		--cov app \
		--cov src \
		--cov-report term-missing

test_e2e: env_dev ## Run local end-to-end test (mocked externals, writes XLSX to tmp)
	uv run --no-sync -- pytest tests/local_e2e -v -s

local_event: # See tests/sample_event.json for body
	curl -XPOST "http://localhost:9000/2015-03-31/functions/function/invocations" \
		-d '{}'

##############
### Docker ###
##############

docker_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

docker_local: docker_local_down docker_local_up

docker_local_up:
	$(DOCKER_COMPOSE) up --build -d function

docker_local_down:
	$(DOCKER_COMPOSE) down function --volumes --remove-orphans

docker_test: docker_test_down docker_test_up

docker_test_up:
	$(DOCKER_COMPOSE) up \
		--exit-code-from lint-and-test \
		--abort-on-container-exit \
		--build lint-and-test

docker_test_down:
	$(DOCKER_COMPOSE) down lint-and-test --volumes --remove-orphans

docker_test_integration: docker_test_integration_down docker_test_integration_up
test_integration: docker_test_integration

docker_test_integration_up:
	$(DOCKER_COMPOSE) up -d lambda
	$(DOCKER_COMPOSE) up \
		--exit-code-from integration-test \
		--abort-on-container-exit \
		--build integration-test

docker_test_integration_down:
	$(DOCKER_COMPOSE) down lambda --volumes --remove-orphans
	$(DOCKER_COMPOSE) down integration-test --volumes --remove-orphans

docker_test_local_integration: docker_test_integration_down docker_test_local_integration_up

docker_test_local_integration_up:
	$(DOCKER_COMPOSE) up \
		--exit-code-from integration-test \
		--abort-on-container-exit \
		--build integration-test

docker_down: docker_test_down docker_test_integration_down

###########
### UAT ###
###########

OUTPUT ?= ./ToE-lambda-output.xlsx

uat: ## Invoke QA lambda and download output XLSX, then verify calculations
	./scripts/uat.sh $(OUTPUT) && $(MAKE) analyze OUTPUT=$(OUTPUT)

analyze: env ## Verify output XLSX calculations and append Analysis column
	uv run --no-sync -- python scripts/analyze_output.py $(OUTPUT)
