.PHONY: help install build publish publish-test clean test lint format check bump-patch bump-minor bump-major release-patch release-minor release-major release

help:
	@echo "Hezor Common - Build & Publish Commands"
	@echo ""
	@echo "Usage:"
	@echo "  make install        - Install dependencies using uv"
	@echo "  make build          - Build the package"
	@echo "  make publish-test   - Publish to TestPyPI"
	@echo "  make publish        - Publish to PyPI (production)"
	@echo "  make clean          - Clean build artifacts"
	@echo "  make test           - Run tests (if any)"
	@echo ""
	@echo "Code Quality:"
	@echo "  make lint           - Run ruff linter"
	@echo "  make format         - Format code with ruff"
	@echo "  make check          - Run all checks (lint + type check + test)"
	@echo ""
	@echo "Version Management:"
	@echo "  make bump-patch     - Bump patch version (0.1.0 -> 0.1.1)"
	@echo "  make bump-minor     - Bump minor version (0.1.0 -> 0.2.0)"
	@echo "  make bump-major     - Bump major version (0.1.0 -> 1.0.0)"
	@echo ""
	@echo "Release (check + build + bump + push + push tags):"
	@echo "  make release-patch  - Release with patch bump"
	@echo "  make release-minor  - Release with minor bump"
	@echo "  make release-major  - Release with major bump"
	@echo ""

install:
	@echo "📦 Installing dependencies..."
	uv sync

build:
	@echo "🔨 Building package..."
	@./scripts/build.sh

publish-test:
	@echo "🚀 Publishing to TestPyPI..."
	@make clean
	@make build
	@./scripts/publish.sh --test

publish:
	@echo "🚀 Publishing to PyPI..."
	@make clean
	@make build
	@./scripts/publish.sh

clean:
	@echo "🧹 Cleaning build artifacts..."
	@rm -rf dist/ build/ *.egg-info
	@find . -type d -name "*.egg-info" -exec rm -rf {} + 2>/dev/null || true
	@find . -type d -name "__pycache__" -exec rm -rf {} + 2>/dev/null || true
	@echo "✅ Clean complete"

test:
	@echo "🧪 Running tests..."
	@uv run pytest

lint:
	@echo "🔍 Running ruff linter..."
	@uv run ruff check .

format:
	@echo "✨ Formatting code with ruff..."
	@uv run ruff format .
	@uv run ruff check --fix .

check:
	@echo "🔍 Running all checks..."
	@echo "\n📝 Step 1/3: Linting with ruff..."
	@uv run ruff check .
	@echo "\n🔎 Step 2/3: Type checking with pyright..."
	@uv run pyright
	@echo "\n🧪 Step 3/3: Running tests..."
	@uv run pytest
	@echo "\n✅ All checks passed!"

bump-patch:
	@echo "⬆️  Bumping patch version..."
	@uv run bump-my-version bump patch

bump-minor:
	@echo "⬆️  Bumping minor version..."
	@uv run bump-my-version bump minor

bump-major:
	@echo "⬆️  Bumping major version..."
	@uv run bump-my-version bump major

# Release targets: check + build + bump version + push + push tags
# bump-my-version auto-creates commit and tag (configured in .bumpversion.toml)

release-patch: _pre-release
	@echo "⬆️  Bumping patch version..."
	@uv run bump-my-version bump patch
	@$(MAKE) _post-release

release-minor: _pre-release
	@echo "⬆️  Bumping minor version..."
	@uv run bump-my-version bump minor
	@$(MAKE) _post-release

release-major: _pre-release
	@echo "⬆️  Bumping major version..."
	@uv run bump-my-version bump major
	@$(MAKE) _post-release

# Alias: make release LEVEL=patch|minor|major (default: patch)
LEVEL ?= patch
release:
	@$(MAKE) release-$(LEVEL)

_pre-release:
	@echo "🚀 Starting release process..."
	@echo ""
	@echo "📝 Step 1/5: Checking for uncommitted changes..."
	@if [ -n "$$(git status --porcelain)" ]; then \
		echo "⚠️  Uncommitted changes detected. Committing all changes..."; \
		git add -A; \
		read -p "Enter commit message: " msg; \
		git commit -m "$$msg"; \
		echo "✅ Changes committed."; \
	else \
		echo "✅ Working tree is clean."; \
	fi
	@echo ""
	@echo "📝 Step 2/5: Running checks (lint + type check + test)..."
	@uv run ruff check .
	@uv run pyright
	@uv run pytest
	@echo "✅ All checks passed!"
	@echo ""
	@echo "📝 Step 3/5: Building package..."
	@rm -rf dist/ build/
	@uv build
	@echo "✅ Build successful!"
	@echo ""
	@echo "📝 Step 4/5: Bumping version..."

_post-release:
	@echo "✅ Version bumped, commit and tag created."
	@echo ""
	@echo "📝 Step 5/5: Pushing to remote..."
	@git push
	@git push origin $$(git describe --tags --abbrev=0)
	@echo ""
	@echo "🎉 Release complete!"
	@echo "   Version: $$(uv run bump-my-version show current_version 2>/dev/null || grep 'current_version' .bumpversion.toml | head -1 | cut -d'"' -f2)"
	@echo "   Tag:     $$(git describe --tags --abbrev=0)"
