.PHONY: build test lint green pypi clean

UV := $(shell command -v uv 2>/dev/null)
export UV_PROJECT_ENVIRONMENT := env

build:
ifndef UV
	curl -LsSf https://astral.sh/uv/install.sh | sh
	export PATH="$$HOME/.local/bin:$$PATH"
endif
	uv sync --all-extras

test: build
	uv run pytest tests/ -v

lint: build
	uv run ruff check src/ tests/
	uv run pyright src/
	uv run green check src/ tests/

green: build
	uv run green check $(wordlist 2,$(words $(MAKECMDGOALS)),$(MAKECMDGOALS))

pypi: build
	@if [ -z "$(Zato_PyPI_Token_Zato_Green)" ]; then \
		echo "Error: Zato_PyPI_Token_Zato_Green env var not set."; \
		exit 1; \
	fi
	@current=$$(grep '^version' pyproject.toml | sed 's/.*"\(.*\)"/\1/'); \
	major=$$(echo $$current | cut -d. -f1); \
	minor=$$(echo $$current | cut -d. -f2); \
	patch=$$(echo $$current | cut -d. -f3); \
	next=$$((patch + 1)); \
	new_version="$$major.$$minor.$$next"; \
	sed -i "s/^version = \"$$current\"/version = \"$$new_version\"/" pyproject.toml; \
	echo "Version: $$current -> $$new_version"
	uv sync --all-extras
	rm -rf dist
	uv build
	uv publish --token $(Zato_PyPI_Token_Zato_Green)

clean:
	rm -rf env __pycache__ .pytest_cache src/*.egg-info dist .ruff_cache

%:
	@:
