.PHONY: help env lint fmt up down ps logs execute restart test docker_lint_and_test
.DEFAULT_GOAL := help

# define root project dir
ROOT_DIR:=$(shell dirname $(realpath $(firstword $(MAKEFILE_LIST))))

help:
	@fgrep -h "##" $(MAKEFILE_LIST) | fgrep -v fgrep | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}'

clean: ## Clean environment
	rm -rf .venv .cache .pytest_cache .ruff_cache .coverage coverage.xml pyunit.xml
	find . | grep -E '__pycache__|\.pyc|\.pyo$\' | xargs rm -rf

env: ## Install dev requirements
	uv sync --locked

lint: ## Run code linters
	uv run pyright
	uv run ruff check
	uv run ruff format --check

fmt: ## Run code formatters
	uv run ruff check --fix
	uv run ruff format

up:  ## Start container
	docker compose up function --build -d

down: ## Shutdown docker app
	docker compose down

ps: ## List all processes
	docker compose ps

logs: ## View logs
	docker compose logs -f

execute: ## Execute function
	curl --request POST \
	  --url http://localhost:9000/2015-03-31/functions/function/invocations \
	  --header 'Content-Type: application/json' \
	  --data @tests/s3_event.json

restart:  ## Load changes
	docker compose restart function

test:  ## Lint and test
	docker compose up --build lint-and-test

docker_lint_and_test:  ## Run linters and unit tests inside docker
	docker compose run --rm --build lint-and-test
