# enkindler-cli — canonical command set.
#
# Both humans and CI use these targets so there is one source of truth.
# `make help` lists everything.

PYTHON ?= python3
VENV   ?= .venv

# venv layout differs on Windows (Scripts/) vs POSIX (bin/).
ifeq ($(OS),Windows_NT)
    VENV_BIN := $(VENV)/Scripts
    PYTHON   := python
else
    VENV_BIN := $(VENV)/bin
endif
PIP := $(VENV_BIN)/pip
PY  := $(VENV_BIN)/python
ENK := $(VENV_BIN)/enkindler

# Backend OpenAPI spec — used by `make regen-sdk`. Override with
# `make regen-sdk OPENAPI_URL=http://localhost:5179/api/openapi.json`
# when iterating on the backend locally.
OPENAPI_URL ?= https://enkindler.enkindl.com/api/openapi.json

# Where the auto-generated openapi-python-client output lives.
# Gitignored — regenerated from the backend spec on demand.
SDK_DIR := src/enkindler_cli/_generated

.DEFAULT_GOAL := help
.PHONY: help setup install dev test lint format typecheck build clean publish-test publish regen-sdk regen-sdk-local smoke version

help:                  ## Show this help.
	@awk 'BEGIN { FS = ":.*?## " } /^[a-zA-Z0-9_-]+:.*?## / { printf "  \033[36m%-16s\033[0m %s\n", $$1, $$2 }' $(MAKEFILE_LIST)

# ─── Local dev setup ─────────────────────────────────────────────────────────

$(VENV_BIN)/python:
	$(PYTHON) -m venv $(VENV)
	$(PIP) install --upgrade pip

setup: $(VENV_BIN)/python  ## Create virtualenv and install runtime deps only.
	$(PIP) install -e .

install: $(VENV_BIN)/python  ## Alias for `setup` + dev extras.
	$(PIP) install -e '.[dev]'

dev: install           ## Same as `install` — for muscle memory.

# ─── Quality gates ───────────────────────────────────────────────────────────

test: install          ## Run the test suite.
	$(VENV_BIN)/pytest

lint: install          ## Lint with ruff (no auto-fix).
	$(VENV_BIN)/ruff check .

format: install        ## Auto-format and apply safe ruff fixes.
	$(VENV_BIN)/ruff check --fix .
	$(VENV_BIN)/ruff format .

typecheck:             ## (Stub) — add when we adopt a type checker.
	@echo "No type checker configured yet. Add mypy or pyright when ready."

smoke: install         ## Cheap end-to-end check that the script entrypoint works.
	$(ENK) --version
	$(ENK) --help > /dev/null

version: install       ## Print the package version.
	@$(ENK) --version

# ─── Build & publish ─────────────────────────────────────────────────────────

build: install         ## Build sdist + wheel into ./dist/
	$(PIP) install --quiet build
	$(PY) -m build

publish-test: build    ## Upload to TestPyPI (requires TWINE_PASSWORD env).
	$(PIP) install --quiet twine
	$(VENV_BIN)/twine upload --repository testpypi dist/*

publish: build         ## Upload to PyPI (requires TWINE_PASSWORD env).
	$(PIP) install --quiet twine
	$(VENV_BIN)/twine upload dist/*

# ─── SDK regen from backend OpenAPI spec ─────────────────────────────────────

regen-sdk: install     ## Regenerate the typed SDK from the backend OpenAPI spec.
	$(PIP) install --quiet openapi-python-client
	rm -rf $(SDK_DIR)
	$(VENV_BIN)/openapi-python-client generate \
		--url $(OPENAPI_URL) \
		--output-path $(SDK_DIR) \
		--meta none \
		--overwrite
	@echo "SDK regenerated into $(SDK_DIR) from $(OPENAPI_URL)"

regen-sdk-local:       ## Regenerate the SDK against a locally-running backend (port 5179).
	$(MAKE) regen-sdk OPENAPI_URL=http://localhost:5179/api/openapi.json

# ─── Housekeeping ────────────────────────────────────────────────────────────

clean:                 ## Remove build artifacts and caches (keeps the venv).
	rm -rf build dist *.egg-info
	rm -rf .pytest_cache .ruff_cache .mypy_cache
	find . -type d -name __pycache__ -prune -exec rm -rf {} +

clean-all: clean       ## Also delete the virtualenv.
	rm -rf $(VENV)
