PYTHON    := $(if $(filter Windows_NT,$(OS)),.venv/Scripts/python.exe,.venv/bin/python)

# Default workspace for single-WS shorthands. Override with `WS=<name>`.
WS ?= $(shell $(PYTHON) -c "import ws; print(next(iter(ws.REGISTRY)))")

# Per-workspace Noxus workflow-name prefix from ws/__init__.py.
PREFIX = $(shell $(PYTHON) -c "import ws; print(ws.PREFIX['$(WS)'])")

.DEFAULT_GOAL := help

help: ## show this help
	@echo "Targets (override WS=<workspace>, default: $(WS)):"
	@awk 'BEGIN {FS = ":.*?## "} /^[a-zA-Z][a-zA-Z0-9_-]*:.*?## / {printf "  %-16s %s\n", $$1, $$2}' $(MAKEFILE_LIST) | sort

test: ## offline tests with coverage
	$(PYTHON) -m pytest -v

check: ## lint + format check (mirrors CI)
	$(PYTHON) -m ruff check .
	$(PYTHON) -m ruff format --check .

fix: ## auto-fix lint and format
	$(PYTHON) -m ruff check --fix .
	$(PYTHON) -m ruff format .

hooks: ## install git hooks so ruff runs on commit
	$(PYTHON) -m pre_commit install
	$(PYTHON) -m pre_commit autoupdate

run: ## live run: make run WF=<wf> [WS=<ws>]
ifndef WF
	$(error Usage: make run WF=<wf> [WS=<ws>])
endif
	$(PYTHON) bin/dev.py --ws $(WS) $(WF)

push: ## push one workflow: make push WF=<wf> [WS=<ws>]
ifndef WF
	$(error Usage: make push WF=<wf> [WS=<ws>])
endif
	$(PYTHON) ws/$(WS)/workflows/$(WF).py

push-all: ## push every workflow of one workspace
	$(PYTHON) bin/deploy.py $(WS)

deploy: ## push every workflow of every workspace
	$(PYTHON) bin/deploy.py

deploy-staging: ## switch to staging .env, then deploy
	noxuslab env use staging
	$(PYTHON) bin/deploy.py

deploy-prod: ## switch to prod .env, then deploy
	noxuslab env use prod
	$(PYTHON) bin/deploy.py

.PHONY: help test check fix hooks run push push-all deploy deploy-staging deploy-prod
