SHELL := bash
.SHELLFLAGS := -e -x -c

.PHONY: install
install: ## Set up environment
	@echo "🚀 Creating virtual environment using uv"
	uv sync --group dev --frozen

check-version:
	@echo "🔍 Checking if a Git tag exists..."
	@if git describe --tags --abbrev=0 >/dev/null 2>&1; then \
	    VERSION=$$(git describe --tags --abbrev=0); \
	    echo "✅ Git tag found: $$VERSION"; \
	else \
	    echo "❌ No Git tag found. Please create one with: git tag v0.1.0"; \
	    exit 1; \
	fi

.PHONY: check
check: ## Run all code quality checks
	@echo "🚀 Checking lock file consistency"
	uv lock --locked
	@echo "🚀 Running pre-commit hooks (ruff, mypy, vulture, safety, etc.)"
	uv run pre-commit run --all-files

.PHONY: test
test: ## Run tests using tox
	@echo "🚀 Testing code: Running tox across Python versions"
	tox

.PHONY: test-local
test-local: ## Run tests in current Python environment using uv
	@echo "🚀 Testing code locally"
	uv run python -m pytest -rvx --setup-show tests

.PHONY: build
build: clean ## Build package using uv
	@echo "🚀 Building project"
	uv build

.PHONY: clean
clean: ## Clean build artifacts
	@echo "🚀 Removing build artifacts"
	rm -rf dist *.egg-info build

.PHONY: version
version: ## Print the current project version
	uv run hatch version

.PHONY: tag
tag: ## Create a Git tag from the current version
	@echo "🏷 Tagging version"
	git tag v$(shell uv run hatch version)
	git push --tags

.PHONY: publish
publish: ## Publish to production PyPI
	@echo "🚀 Publishing to PyPI"
	UV_PUBLISH_TOKEN=$(PYPI_TOKEN) uv publish --publish-url=https://upload.pypi.org/legacy/ --no-cache

.PHONY: publish-test
publish-test: ## Publish to TestPyPI (for dry runs)
	@echo "🚀 Publishing to TestPyPI"
	UV_PUBLISH_TOKEN=$(TEST_PYPI_TOKEN) uv publish --publish-url=https://test.pypi.org/legacy/ --no-cache

.PHONY: build-and-publish
build-and-publish: build publish ## Build and publish in one step
.PHONY: help
help:
	uv run python -c "import re; \
	[[print(f'\033[36m{m[0]:<20}\033[0m {m[1]}') for m in re.findall(r'^([a-zA-Z_-]+):.*?## (.*)$$', open(makefile).read(), re.M)] for makefile in ('$(MAKEFILE_LIST)').strip().split()]"

.DEFAULT_GOAL := help
