PYTHON ?= python3
UV ?= uv
VERSION := $(shell $(PYTHON) -c "import tomllib; print(tomllib.load(open('pyproject.toml','rb'))['project']['version'])")
TAG ?= v$(VERSION)
IMAGE ?= ghcr.io/reignonu13/jiraxray-cli
WHEEL := dist/jiraxray_cli-$(VERSION)-py3-none-any.whl
SDIST := dist/jiraxray_cli-$(VERSION).tar.gz

.PHONY: help sync lint typecheck test check build smoke package clean install-local install-wheel publish-testpypi publish-pypi tag github-release docker-build docker-run ghcr-push publish-github

help:
	@echo "Targets:"
	@echo "  make sync              Install project and dev dependencies"
	@echo "  make check             Run lint, type checks, and unit tests"
	@echo "  make build             Build wheel and source distribution"
	@echo "  make smoke             Smoke-test installed wheel and source distribution"
	@echo "  make package           Run check, build, and smoke"
	@echo "  make install-local     Install package from the local checkout"
	@echo "  make install-wheel     Install package from the built wheel"
	@echo "  make github-release    Upload dist artifacts to a GitHub Release"
	@echo "  make docker-build      Build the GHCR CLI container image"
	@echo "  make ghcr-push         Push the GHCR CLI container image"
	@echo "  make publish-github    Build, release artifacts, and push GHCR image"
	@echo "  make publish-testpypi  Publish dist artifacts to TestPyPI"
	@echo "  make publish-pypi      Publish dist artifacts to PyPI"

sync:
	$(UV) sync --dev

lint:
	$(UV) run ruff check .

typecheck:
	$(UV) run mypy src

test:
	$(UV) run pytest

check: lint typecheck test

build:
	$(UV) build

smoke: build
	$(UV) run --isolated --no-project --with $(WHEEL) tests/smoke_package.py
	$(UV) run --isolated --no-project --with $(SDIST) tests/smoke_package.py

package: check build smoke

clean:
	rm -rf dist build *.egg-info

install-local:
	$(PYTHON) -m pip install .

install-wheel: build
	$(PYTHON) -m pip install $(WHEEL)

publish-testpypi: package
	$(UV) publish --publish-url https://test.pypi.org/legacy/

publish-pypi: package
	$(UV) publish

tag:
	git tag -a $(TAG) -m $(TAG)
	git push origin $(TAG)

github-release: package
	gh release create $(TAG) dist/* --title "$(TAG)" --generate-notes --verify-tag

docker-build: build
	docker build --build-arg WHEEL=$(WHEEL) -t $(IMAGE):$(VERSION) -t $(IMAGE):latest .

docker-run: docker-build
	docker run --rm $(IMAGE):$(VERSION) --version

ghcr-push: docker-build
	docker push $(IMAGE):$(VERSION)
	docker push $(IMAGE):latest

publish-github: package github-release ghcr-push
