UV ?= uv
PROJECT_DIR ?= .

.PHONY: run check format test install clean publish

# The app and UI both read the version from installed package metadata, so
# pyproject.toml is the only place it is written down.

run:
	$(UV) run smfetch

format:
	$(UV) run ruff format $(PROJECT_DIR)

check:
	$(UV) run ruff check --fix --select I,F,UP,B $(PROJECT_DIR)
	$(UV) run ty check $(PROJECT_DIR)

# Fully offline: the scrapers run against captured pages and the rest against a
# fake site, so this never touches the network or your real songs directory.
test:
	$(UV) run pytest

install: clean format check
	$(UV) sync
	$(MAKE) test

clean:
	find $(PROJECT_DIR) -type d -name "__pycache__" -exec rm -rf {} + 2>/dev/null || true
	find $(PROJECT_DIR) -type f -name "*.py[co]" -delete 2>/dev/null || true
	rm -rf $(PROJECT_DIR)/.ruff_cache $(PROJECT_DIR)/.ty_cache $(PROJECT_DIR)/.mypy_cache $(PROJECT_DIR)/.pyright $(PROJECT_DIR)/.eggs 2>/dev/null || true
	rm -rf $(PROJECT_DIR)/dist $(PROJECT_DIR)/build $(PROJECT_DIR)/*.egg-info 2>/dev/null || true
	rm -rf $(PROJECT_DIR)/.pytest_cache $(PROJECT_DIR)/.coverage* $(PROJECT_DIR)/htmlcov 2>/dev/null || true

publish:
	@CURRENT_VERSION=$$($(UV) version --short); \
	echo "Current version: $$CURRENT_VERSION"; \
	read -p "Enter new version (or press Enter to keep current): " NEW_VERSION; \
	if [ -n "$$NEW_VERSION" ] && [ "$$NEW_VERSION" != "$$CURRENT_VERSION" ]; then \
		echo "Updating version to $$NEW_VERSION..."; \
		$(UV) version "$$NEW_VERSION"; \
	elif [ -n "$$NEW_VERSION" ] && [ "$$NEW_VERSION" = "$$CURRENT_VERSION" ]; then \
		echo "Version unchanged (same as current)."; \
	else \
		echo "Using current version: $$CURRENT_VERSION"; \
	fi; \
	FINAL_VERSION=$$($(UV) version --short); \
	$(MAKE) install; \
	$(UV) build; \
	$(UV) publish; \
	if [ -n "$$(git status --porcelain -- pyproject.toml uv.lock)" ]; then \
		echo "Committing version changes..."; \
		git add pyproject.toml uv.lock; \
		git commit -m "Release $$FINAL_VERSION"; \
		echo "Pushing to remote..."; \
		git push origin HEAD; \
	else \
		echo "No version-related changes; skipping commit and push."; \
	fi
