# ─────────────────────────────────────────────────────────────────────────────
# data-hydrator — Makefile
# ─────────────────────────────────────────────────────────────────────────────
# Usage:
#   make              → show this help
#   make install      → uv sync (all deps including dev)
#   make test         → run test suite
#   make test-cov     → run tests with HTML coverage report
#   make lint         → ruff check + mypy
#   make fmt          → auto-format with ruff
#   make build        → build wheel + sdist into dist/
#   make publish      → publish to PyPI       (set PYPI_TOKEN)
#   make publish-test → publish to TestPyPI   (set PYPI_TOKEN)
#   make bump-patch   → 0.2.0 → 0.2.1
#   make bump-minor   → 0.2.0 → 0.3.0
#   make bump-major   → 0.2.0 → 1.0.0
#   make version      → print current version
#   make clean        → remove build artefacts + caches
# ─────────────────────────────────────────────────────────────────────────────

.DEFAULT_GOAL := help
.PHONY: help install test test-cov lint fmt \
        build publish publish-test \
        bump-patch bump-minor bump-major _git-tag-version \
        version clean clean-dist


# ── Config ───────────────────────────────────────────────────────────────────

UV      := uv
PACKAGE := data_hydrator

# Read version straight from pyproject.toml — no tool dependency
VERSION := $(shell grep '^version' pyproject.toml | head -1 | sed 's/version = "\(.*\)"/\1/')

BOLD  := $(shell tput bold 2>/dev/null || true)
RESET := $(shell tput sgr0 2>/dev/null || true)
GREEN := $(shell tput setaf 2 2>/dev/null || true)
CYAN  := $(shell tput setaf 6 2>/dev/null || true)


# ── Help ─────────────────────────────────────────────────────────────────────

help:
	@echo ""
	@echo "$(BOLD)data-hydrator $(VERSION)$(RESET)"
	@echo ""
	@echo "$(CYAN)Development$(RESET)"
	@echo "  make install        uv sync — install all deps including dev group"
	@echo "  make test           Run test suite"
	@echo "  make test-cov       Run tests with HTML coverage report"
	@echo "  make lint           ruff check + mypy type check"
	@echo "  make fmt            Auto-format with ruff"
	@echo ""
	@echo "$(CYAN)Build & publish$(RESET)"
	@echo "  make build          Build wheel + sdist → dist/"
	@echo "  make publish        Publish to PyPI      (set PYPI_TOKEN env var)"
	@echo "  make publish-test   Publish to TestPyPI  (set PYPI_TOKEN env var)"
	@echo ""
	@echo "$(CYAN)Version bumping$(RESET)"
	@echo "  make bump-patch     $(VERSION) → patch  (e.g. 0.2.0 → 0.2.1)"
	@echo "  make bump-minor     $(VERSION) → minor  (e.g. 0.2.0 → 0.3.0)"
	@echo "  make bump-major     $(VERSION) → major  (e.g. 0.2.0 → 1.0.0)"
	@echo "  make version        Print current version"
	@echo ""
	@echo "  Pass NO_GIT=1 to skip the commit + tag step:"
	@echo "    make bump-minor NO_GIT=1"
	@echo ""
	@echo "$(CYAN)Housekeeping$(RESET)"
	@echo "  make clean          Remove dist/, caches, __pycache__"
	@echo ""


# ── Install ──────────────────────────────────────────────────────────────────

install:
	$(UV) sync
	@echo "$(GREEN)✓ Environment synced$(RESET)"


# ── Test ─────────────────────────────────────────────────────────────────────

test:
	$(UV) run pytest tests/ -v --tb=short

test-cov:
	$(UV) run pytest tests/ -v --tb=short \
		--cov=$(PACKAGE) \
		--cov-report=term-missing \
		--cov-report=html:htmlcov
	@echo "$(GREEN)✓ Coverage report → htmlcov/index.html$(RESET)"


# ── Lint & format ────────────────────────────────────────────────────────────

lint:
	@echo "$(BOLD)ruff check$(RESET)"
	$(UV) run ruff check $(PACKAGE)/ examples/
	@echo "$(BOLD)mypy$(RESET)"
	$(UV) run mypy $(PACKAGE)/ --ignore-missing-imports
	@echo "$(GREEN)✓ Lint passed$(RESET)"

fmt:
	$(UV) run ruff format $(PACKAGE)/ examples/
	$(UV) run ruff check --fix $(PACKAGE)/ examples/
	@echo "$(GREEN)✓ Formatted$(RESET)"


# ── Build ────────────────────────────────────────────────────────────────────

build: clean-dist
	$(UV) build
	@echo "$(GREEN)✓ Built:$(RESET)"
	@ls -lh dist/


# ── Publish ──────────────────────────────────────────────────────────────────
#
# uv publish reads UV_PUBLISH_TOKEN automatically, or pass PYPI_TOKEN here.
# For TestPyPI, uv publish --publish-url overrides the registry endpoint.

publish: build
	@echo "$(BOLD)Publishing v$(VERSION) to PyPI…$(RESET)"
ifdef PYPI_TOKEN
	$(UV) publish --token $(PYPI_TOKEN)
else
	$(UV) publish
endif
	@echo "$(GREEN)✓ Published v$(VERSION) to PyPI$(RESET)"

publish-test: build
	@echo "$(BOLD)Publishing v$(VERSION) to TestPyPI…$(RESET)"
ifdef PYPI_TOKEN
	$(UV) publish \
		--publish-url https://test.pypi.org/legacy/ \
		--token $(PYPI_TOKEN)
else
	$(UV) publish --publish-url https://test.pypi.org/legacy/
endif
	@echo "$(GREEN)✓ Published v$(VERSION) to TestPyPI$(RESET)"
	@echo ""
	@echo "Install from TestPyPI:"
	@echo "  pip install --index-url https://test.pypi.org/simple/ data-hydrator==$(VERSION)"


# ── Version bumping ──────────────────────────────────────────────────────────
#
# `uv version --bump <rule>` edits pyproject.toml in-place.
# After bumping, _git-tag-version commits the file and creates an annotated tag.
# Skip git steps with NO_GIT=1:
#   make bump-minor NO_GIT=1

bump-patch:
	$(UV) version --bump patch
	$(eval NEW_VERSION := $(shell grep '^version' pyproject.toml | head -1 | sed 's/version = "\(.*\)"/\1/'))
	@echo "$(GREEN)✓ Bumped to v$(NEW_VERSION)$(RESET)"
	@$(MAKE) --no-print-directory _git-tag-version NEW_VERSION=$(NEW_VERSION)

bump-minor:
	$(UV) version --bump minor
	$(eval NEW_VERSION := $(shell grep '^version' pyproject.toml | head -1 | sed 's/version = "\(.*\)"/\1/'))
	@echo "$(GREEN)✓ Bumped to v$(NEW_VERSION)$(RESET)"
	@$(MAKE) --no-print-directory _git-tag-version NEW_VERSION=$(NEW_VERSION)

bump-major:
	$(UV) version --bump major
	$(eval NEW_VERSION := $(shell grep '^version' pyproject.toml | head -1 | sed 's/version = "\(.*\)"/\1/'))
	@echo "$(GREEN)✓ Bumped to v$(NEW_VERSION)$(RESET)"
	@$(MAKE) --no-print-directory _git-tag-version NEW_VERSION=$(NEW_VERSION)

# Internal target — commit + tag (skipped when NO_GIT=1)
_git-tag-version:
ifndef NO_GIT
	git add pyproject.toml
	git commit -m "chore: bump version to v$(NEW_VERSION)"
	git tag -a "v$(NEW_VERSION)" -m "Release v$(NEW_VERSION)"
	@echo "$(GREEN)✓ Tagged v$(NEW_VERSION)$(RESET)"
	@echo "Push with:  git push && git push --tags"
endif


# ── Version info ─────────────────────────────────────────────────────────────

version:
	@echo $(VERSION)


# ── Clean ────────────────────────────────────────────────────────────────────

clean-dist:
	rm -rf dist/

clean:
	rm -rf dist/ build/ *.egg-info .eggs/
	rm -rf htmlcov/ .coverage coverage.xml
	rm -rf .mypy_cache/ .ruff_cache/ .pytest_cache/
	find . -type d -name __pycache__ -exec rm -rf {} + 2>/dev/null || true
	find . -type f -name "*.pyc" -delete 2>/dev/null || true
	@echo "$(GREEN)✓ Cleaned$(RESET)"
