# profitstone_sdk — pstone Python SDK
#
#   make help
#   make sync
#   make test
#   make build
#   make publish        # reads UV_PUBLISH_TOKEN from .env
#   make publish-test   # TestPyPI

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

# Local secrets for publish (command-line overrides .env)
ifneq (,$(wildcard .env))
include .env
endif
export UV_PUBLISH_TOKEN

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

.DEFAULT_GOAL := help

help:
	@echo "profitstone_sdk ($(PACKAGE) $(VERSION))"
	@echo ""
	@echo "Development:"
	@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 ""
	@echo "Publish:"
	@echo "  make publish       Upload to PyPI (UV_PUBLISH_TOKEN in .env)"
	@echo "  make publish-test  Upload to TestPyPI"
	@echo ""
	@echo "Setup:"
	@echo "  cp .env.example .env   # then set UV_PUBLISH_TOKEN"

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 .env ]; then \
			echo "Create .env from the example:"; \
			echo "  cp .env.example .env"; \
		else \
			echo "Add UV_PUBLISH_TOKEN to .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)"
