# profitstone_sdk / python — PyPI package `pstone`
#
# Prefer root Makefile: `make python-build`, `make python-publish`, …
# Or from this directory: `make build`, `make publish`

UV ?= uv
PACKAGE := pstone
VERSION := $(shell grep '^version = ' pyproject.toml | cut -d'"' -f2)

# Repo-root .env for publish tokens (override with env/CLI)
ROOT_DIR := $(abspath ..)
ifneq (,$(wildcard $(ROOT_DIR)/.env))
include $(ROOT_DIR)/.env
endif
export UV_PUBLISH_TOKEN

.PHONY: help sync test build clean publish publish-test version check-token

.DEFAULT_GOAL := help

help:
	@echo "python/ ($(PACKAGE) $(VERSION))"
	@echo ""
	@echo "  make sync           Install deps (uv sync --dev)"
	@echo "  make test           Run pytest"
	@echo "  make build          Build sdist + wheel into dist/"
	@echo "  make clean          Remove dist/ and build artifacts"
	@echo "  make version        Print package version"
	@echo "  make publish        Upload to PyPI (UV_PUBLISH_TOKEN)"
	@echo "  make publish-test   Upload to TestPyPI"

sync:
	$(UV) sync --dev

test: sync
	$(UV) run pytest -q

build: sync
	$(UV) build
	@echo "Built dist/$(PACKAGE)-$(VERSION).tar.gz"
	@echo "Built dist/$(PACKAGE)-$(VERSION)-py3-none-any.whl"

clean:
	rm -rf dist build src/*.egg-info
	find . -type d -name __pycache__ -prune -exec rm -rf {} + 2>/dev/null || true

version:
	@echo $(VERSION)

check-token:
	@if [ -z "$(UV_PUBLISH_TOKEN)" ]; then \
		echo "Error: UV_PUBLISH_TOKEN is not set."; \
		if [ ! -f "$(ROOT_DIR)/.env" ]; then \
			echo "Create .env from the example:"; \
			echo "  cp .env.example .env"; \
		else \
			echo "Add UV_PUBLISH_TOKEN to repo-root .env"; \
		fi; \
		exit 1; \
	fi

publish: test build check-token
	$(UV) publish
	@echo "Published $(PACKAGE) $(VERSION) to PyPI"
	@echo "Verify: pip install $(PACKAGE)==$(VERSION)"

publish-test: test build check-token
	UV_PUBLISH_URL=https://test.pypi.org/legacy/ $(UV) publish
	@echo "Published $(PACKAGE) $(VERSION) to TestPyPI"
	@echo "Verify: pip install -i https://test.pypi.org/simple/ $(PACKAGE)==$(VERSION)"
