LANG=en_US.utf-8
export LANG

# Bridge the org-standard JFrog vars to what native uv expects (index name: jfrog).
# Username is optional -- a bare token authenticates against the virtual-pypi index.
UV_INDEX_JFROG_USERNAME ?= $(JFROG_READ_USER)
UV_INDEX_JFROG_PASSWORD ?= $(JFROG_READ_TOKEN)
export UV_INDEX_JFROG_USERNAME
export UV_INDEX_JFROG_PASSWORD

.PHONY: sync
sync: ## Create/update the local .venv (deps + dev group)
	uv sync --all-extras --group dev

.PHONY: uv.lock
uv.lock: ## Regenerate uv.lock from pyproject.toml
	uv lock

.PHONY: devsetup
devsetup: sync setup-pre-commit ## one time setup for devs
	make tests

.DEFAULT_GOAL := help
.PHONY: help
help: ## Show this help.
	# from https://marmelab.com/blog/2016/02/29/auto-documented-makefile.html
	@grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}'

.PHONY: fetch-onc-data
fetch-onc-data: ## Download the ONC-derived test data the ONC regression tests read (tests/fixtures/onc/) -- run before `make tests` to include them; they skip (not fail) if this hasn't been run
	uv run python scripts/fetch_onc_test_data.py

.PHONY: tests
tests: ## Runs all the tests
	uv run pytest .

.PHONY: clean-pre-commit
clean-pre-commit: ## removes pre-commit hook
	uv run pre-commit uninstall

.PHONY: setup-pre-commit
setup-pre-commit: ## Install the pre-commit git hook (uv-managed)
	uv run pre-commit install

.PHONY: run-pre-commit
run-pre-commit: ## Run all pre-commit hooks over all files (no install needed)
	uv run --frozen --no-sync pre-commit run --all-files

.PHONY: third-party-notices
third-party-notices: ## Regenerate THIRD-PARTY-NOTICES.md from the current dependency tree
	@echo "# Third-Party Notices" > THIRD-PARTY-NOTICES.md
	@echo "" >> THIRD-PARTY-NOTICES.md
	@echo "This file is generated by \`make third-party-notices\` — do not hand-edit." >> THIRD-PARTY-NOTICES.md
	@echo "" >> THIRD-PARTY-NOTICES.md
	@echo "## Python dependencies" >> THIRD-PARTY-NOTICES.md
	@echo "" >> THIRD-PARTY-NOTICES.md
	uv run pip-licenses --format=markdown --with-urls >> THIRD-PARTY-NOTICES.md

.PHONY: dist
dist: ## Build the sdist + wheel into dist/
	rm -rf dist/
	uv build

.PHONY: testpackage
testpackage: dist ## Upload the built distribution to TestPyPI (token in TWINE_PASSWORD)
	uv run --frozen --no-sync twine upload -u __token__ --repository testpypi dist/*

.PHONY: package
package: dist ## Upload the built distribution to PyPI (token in TWINE_PASSWORD)
	uv run --frozen --no-sync twine upload -u __token__ --repository pypi dist/*
