# Windy Fly developer + release Makefile.
#
# Convenience targets — keep this file thin. Anything beyond ~5 lines
# of shell belongs in scripts/, not here.

.PHONY: help test lint typecheck check release-tag image image-run release-dry-run

help:
	@echo "Common targets:"
	@echo "  make test          — full pytest suite"
	@echo "  make lint          — ruff check src/"
	@echo "  make typecheck     — mypy src/"
	@echo "  make check         — lint + typecheck + test"
	@echo "  make image         — build local Docker image (windy-fly:dev)"
	@echo "  make image-run     — run the local image w/ env from ~/.windy/windy-0.env"
	@echo ""
	@echo "Release flow:"
	@echo "  make release-dry-run — print the tag + commands without acting"
	@echo "  make release-tag VERSION=0.5.2 — bump __version__, tag v0.5.2,"
	@echo "      push tag (CI publishes PyPI + GHCR image automatically)"

test:
	.venv/bin/python -m pytest tests/ -q

lint:
	.venv/bin/python -m ruff check src/

typecheck:
	.venv/bin/python -m mypy src/windyfly/ --ignore-missing-imports

check: lint typecheck test

# ── Image ──────────────────────────────────────────────────────────

GIT_SHA := $(shell git rev-parse --short HEAD 2>/dev/null || echo unknown)
BUILD_DATE := $(shell date -u +%Y-%m-%dT%H:%M:%SZ)

image:
	docker build \
	  --build-arg GIT_SHA=$(GIT_SHA) \
	  --build-arg BUILD_DATE=$(BUILD_DATE) \
	  --build-arg VERSION=dev \
	  -t windy-fly:dev .

image-run:
	docker run --rm -it \
	  --env-file ~/.windy/windy-0.env \
	  -p 3000:3000 \
	  windy-fly:dev

# ── Release ────────────────────────────────────────────────────────

release-dry-run:
	@if [ -z "$(VERSION)" ]; then \
	  echo "FAIL: pass VERSION=x.y.z, e.g. make release-tag VERSION=0.5.2"; \
	  exit 1; \
	fi
	@echo "Would:"
	@echo "  1. sed -i 's/__version__ = .*/__version__ = \"$(VERSION)\"/' src/windyfly/__init__.py"
	@echo "  2. git add src/windyfly/__init__.py"
	@echo "  3. git commit -m 'chore(release): bump to v$(VERSION)'"
	@echo "  4. git tag -a v$(VERSION) -m 'Release v$(VERSION)'"
	@echo "  5. git push origin master --tags"
	@echo
	@echo "  CI then runs the release.yml workflow on tag push:"
	@echo "    - tests across py3.12/3.13/3.14"
	@echo "    - publishes wheel to PyPI"
	@echo "    - builds runtime image and pushes to:"
	@echo "        ghcr.io/sneakyfree/windy-fly:v$(VERSION)"
	@echo "        ghcr.io/sneakyfree/windy-fly:$(VERSION)"
	@echo "        ghcr.io/sneakyfree/windy-fly:latest"
	@echo "    - creates a GitHub Release with autogenerated notes"

release-tag:
	@if [ -z "$(VERSION)" ]; then \
	  echo "FAIL: pass VERSION=x.y.z"; exit 1; \
	fi
	@if ! echo "$(VERSION)" | grep -qE '^[0-9]+\.[0-9]+\.[0-9]+$$'; then \
	  echo "FAIL: VERSION must be semver (e.g. 0.5.2)"; exit 1; \
	fi
	@if ! git diff-index --quiet HEAD --; then \
	  echo "FAIL: working tree dirty; commit or stash first"; exit 1; \
	fi
	@if [ "$$(git rev-parse --abbrev-ref HEAD)" != "master" ]; then \
	  echo "FAIL: release-tag from master only (you are on $$(git rev-parse --abbrev-ref HEAD))"; exit 1; \
	fi
	sed -i 's/^__version__ = .*/__version__ = "$(VERSION)"/' src/windyfly/__init__.py
	git add src/windyfly/__init__.py
	git commit -m "chore(release): bump to v$(VERSION)"
	git tag -a "v$(VERSION)" -m "Release v$(VERSION)"
	git push origin master --tags
	@echo
	@echo "✓ Tagged v$(VERSION). CI will publish PyPI + GHCR image."
	@echo "  Watch: https://github.com/sneakyfree/windy-agent/actions"
