.PHONY: help install outdated upgrade upgrade-dep

help:
	@echo "Useful targets:"
	@echo ""
	@echo "Setup:"
	@echo "  install           - Install the package in editable mode with uv"
	@echo ""
	@echo "Dependencies:"
	@echo "  outdated          - Show outdated Python dependencies (read-only)"
	@echo "  upgrade           - Upgrade all outdated Python dependencies"
	@echo "  upgrade-dep       - Upgrade a single dependency (e.g., make upgrade-dep PKG=boto3)"
	@echo ""

.DEFAULT_GOAL := help

install:
	uv pip install -e .

# Show outdated Python dependencies (read-only, does not modify any files)
outdated:
	@python .github/scripts/check_outdated_deps.py table

# Upgrade all outdated dependencies, bump patch version
upgrade:
	@echo "=========================================================================="
	@echo "Checking for outdated Python dependencies"
	@echo "=========================================================================="
	@echo ""
	@outdated=$$(python .github/scripts/check_outdated_deps.py check); \
	if [ "$$outdated" = "[]" ]; then \
		echo "All dependencies are up to date."; \
		exit 0; \
	fi; \
	echo "$$outdated" | python -c "import sys, json; \
	deps = json.load(sys.stdin); \
	[print(f\"  {d['package']}: {d['current_version']} -> {d['latest_version']}\") for d in deps]"; \
	echo ""; \
	echo "Upgrading..."; \
	echo "$$outdated" | python -c "import sys, json, subprocess; \
	deps = json.load(sys.stdin); \
	[subprocess.run(['python', '.github/scripts/check_outdated_deps.py', 'apply', d['package'], d['current_version'], d['latest_version']], check=True) for d in deps]" && \
	echo "" && \
	echo "Bumping patch version..." && \
	python .github/scripts/check_outdated_deps.py bump-version

# Upgrade a single dependency (e.g., make upgrade-dep PKG=boto3)
upgrade-dep:
	@if [ -z "$(PKG)" ]; then \
		echo "Error: PKG is required"; \
		echo "Usage: make upgrade-dep PKG=<package-name>"; \
		echo "Example: make upgrade-dep PKG=boto3"; \
		exit 1; \
	fi
	@echo "=========================================================================="
	@echo "Upgrading $(PKG)"
	@echo "=========================================================================="
	@echo ""
	@outdated=$$(python .github/scripts/check_outdated_deps.py check); \
	match=$$(echo "$$outdated" | python -c "import sys, json, re; \
	norm = lambda n: re.sub(r'[-_.]+', '-', n).lower(); \
	deps = json.load(sys.stdin); \
	matches = [d for d in deps if norm(d['package']) == norm('$(PKG)')]; \
	print(json.dumps(matches[0]) if matches else '')"); \
	if [ -z "$$match" ]; then \
		echo "$(PKG) is already up to date or not found in pyproject.toml"; \
		exit 0; \
	fi; \
	old=$$(echo "$$match" | python -c "import sys, json; d = json.load(sys.stdin); print(d['current_version'])"); \
	new=$$(echo "$$match" | python -c "import sys, json; d = json.load(sys.stdin); print(d['latest_version'])"); \
	echo "  $(PKG): $$old -> $$new"; \
	echo ""; \
	python .github/scripts/check_outdated_deps.py apply "$(PKG)" "$$old" "$$new" && \
	echo "" && \
	echo "Bumping patch version..." && \
	python .github/scripts/check_outdated_deps.py bump-version
