## Rift Commander — development tasks.
## Run `make` or `make help` for the list.
##
## Windows without GNU make: use `.\make.ps1 <target>`, which mirrors these
## targets. Anything platform-specific lives in `cargo xtask`, not here.

CARGO   ?= cargo
BIN     ?= riftcommander-tui
ARGS    ?=
INPUT_DIR ?= target/release

ifeq ($(OS),Windows_NT)
    RM_RF = powershell -NoProfile -Command "Remove-Item -Recurse -Force -ErrorAction SilentlyContinue"
else
    RM_RF = rm -rf
endif

.DEFAULT_GOAL := help
.PHONY: help tools tools-list build release bin bin-clean run install \
        test test-fast lint fmt fmt-check clippy layering deny check \
        bench fixtures doc audit outdated clean distclean dist package package-linux package-windows release-publish release-tag release-version

ifeq ($(OS),Windows_NT)
help:
	@powershell -NoProfile -File make.ps1 help
else
help: ## Show this list
	@grep -hE '^[a-zA-Z0-9_-]+:.*?## ' $(MAKEFILE_LIST) | awk 'BEGIN {FS = ":.*?## "}; {printf "  \033[1m%-12s\033[0m %s\n", $$1, $$2}'
endif

# --- setup -------------------------------------------------------------------

tools: ## Install the development tools (Linux, macOS and Windows)
	$(CARGO) xtask tools

tools-list: ## Show which development tools are installed
	$(CARGO) xtask tools-list

# --- build -------------------------------------------------------------------

build: ## Debug build, staged into bin/
	$(CARGO) xtask bin

release: ## Optimised build, staged into bin/
	$(CARGO) xtask bin --release

bin: build ## Alias for build

bin-clean: ## Remove the staged binaries
	$(CARGO) xtask bin-clean

package: package-linux ## Package prebuilt Linux binaries as Arch, Debian, RPM and tarball

dist: ## Build/test/package Linux + Windows in containers; write verified artifacts to dist/
	$(CARGO) xtask dist build

package-linux: ## Package prebuilt Linux binaries (INPUT_DIR=target/release)
	$(CARGO) xtask dist linux --input-dir "$(INPUT_DIR)"

package-windows: ## Package prebuilt Windows binaries as ZIP + NSIS (INPUT_DIR=...)
	$(CARGO) xtask dist windows --input-dir "$(INPUT_DIR)"

release-publish: ## Verify and publish dist/ to git.warky.dev (requires registry credentials)
	$(CARGO) xtask dist verify
	$(CARGO) xtask dist publish

release-tag: ## Tag the current version and push main + tag atomically; CI builds/publishes
	$(CARGO) xtask dist tag

release-version: ## Bump patch version, update Cargo.lock, commit, tag and push; CI publishes
	$(CARGO) xtask dist release-version

run: ## Run the terminal build (make run ARGS="--help")
	$(CARGO) run --bin $(BIN) -- $(ARGS)

install: ## Install the terminal binary into the cargo bin directory
	$(CARGO) install --path apps/riftcommander-tui --force

# --- quality -----------------------------------------------------------------

test: ## Run the test suite
	$(CARGO) test --workspace --all-features

test-fast: ## Run the test suite with nextest (make tools)
	$(CARGO) nextest run --workspace --all-features

fmt: ## Format the workspace
	$(CARGO) fmt --all

fmt-check: ## Fail if anything is unformatted
	$(CARGO) fmt --all --check

clippy: ## Lint with warnings denied
	$(CARGO) clippy --workspace --all-targets --all-features -- -D warnings

layering: ## Enforce the architecture rules from LLM/01-architecture.md
	$(CARGO) xtask check-layering

deny: ## Check dependency licences and advisories (make tools)
	$(CARGO) deny check

lint: fmt-check clippy layering ## Formatting, lints and layering

check: lint test deny ## Everything CI runs

# --- development -------------------------------------------------------------

bench: ## Run the benchmark suite
	$(CARGO) xtask bench

fixtures: ## Generate test fixtures
	$(CARGO) xtask fixtures

doc: ## Build and open the API documentation
	$(CARGO) doc --workspace --no-deps --open

audit: ## Security advisories only
	$(CARGO) deny check advisories

outdated: ## Show outdated dependencies (make tools)
	$(CARGO) outdated --workspace --root-deps-only

# --- housekeeping ------------------------------------------------------------

clean: bin-clean ## Remove build artefacts and staged binaries
	$(CARGO) clean

distclean: clean ## Also remove the lockfile
	$(RM_RF) Cargo.lock
