.PHONY: help audit audit-full monitor monitor-simple benchmark benchmark-baseline benchmark-compare optimize verify test agentic-setup agentic-quick agentic-full

help: ## Show available commands
	@echo "Strix Halo Optimization Toolkit"
	@echo ""
	@grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | awk 'BEGIN {FS = ":.*?## "}; {printf "  \033[36m%-22s\033[0m %s\n", $$1, $$2}'

# --- Audit ---
audit: ## Quick system status (single screen)
	@bash bin/audit --quick

audit-full: ## Full system report (saved to data/audits/)
	@bash bin/audit --full

# --- Monitor ---
monitor: ## Launch tmux monitoring dashboard
	@bash bin/monitor --dashboard

monitor-simple: ## Launch amdgpu_top only
	@bash bin/monitor --simple

monitor-install: ## Install monitoring tools (amdgpu_top, btop)
	@bash scripts/monitor/install-tools.sh

monitor-log: ## Start background metric logger
	@bash bin/monitor --log

# --- Benchmark ---
benchmark-setup: ## Ensure toolboxes and test models are ready
	@bash scripts/benchmark/setup.sh

benchmark-baseline: ## Capture pre-optimization baseline (supports ARGS="--max-size 20 --skip-longctx")
	@bash bin/benchmark baseline $(ARGS)

benchmark: ## Run full benchmark suite (supports ARGS="--tag NAME --max-size 20")
	@bash bin/benchmark run $(ARGS)

benchmark-compare: ## Compare two benchmark runs (usage: make benchmark-compare BEFORE=dir AFTER=dir)
	@bash bin/benchmark compare $(BEFORE) $(AFTER)

# --- Serve ---
serve: ## Launch Qwen3.6-35B-A3B UD-Q6_K_XL daily driver (2 slots, 256K ctx)
	@bash bin/serve -m Qwen3.6-35B-A3B-UD-Q6_K_XL.gguf --parallel 2 --ctx 262144 $(ARGS)

serve-custom: ## Launch llama-server with custom model (ARGS="-m MODEL.gguf")
	@bash bin/serve $(ARGS)

serve-ngram: ## Launch with n-gram speculative decoding (ARGS="-m MODEL.gguf")
	@bash bin/serve --ngram $(ARGS)

flush-gpu: ## Kill llama-server/bench processes and drop kernel caches to free unified VRAM
	-@pkill -x llama-server 2>/dev/null || true
	-@pkill -x llama-bench 2>/dev/null || true
	-@pkill -x llama-cli 2>/dev/null || true
	-@podman ps --filter name=llama --format '{{.Names}}' | xargs -r podman stop
	@sync && sudo sysctl vm.drop_caches=3
	@echo "VRAM usage:" && cat /sys/class/drm/card*/device/mem_info_vram_used 2>/dev/null | awk '{printf "  %.2f MiB\n", $$1/1048576}'

# --- Hardware Info ---
hw-bandwidth: ## Measure GPU memory bandwidth and compute (clpeak)
	@clpeak 2>&1

# --- Optimize ---
optimize: ## Interactive optimization walkthrough
	@bash bin/optimize --all

optimize-kernel: ## Configure kernel boot parameters
	@bash scripts/optimize/kernel-params.sh

optimize-tuned: ## Switch to accelerator-performance profile
	@bash scripts/optimize/tuned-profile.sh

optimize-power: ## Apply Phase 2 tuning (ryzenadj, sysctl, THP, RADV)
	@bash scripts/optimize/power-profile.sh

optimize-vram: ## BIOS VRAM guidance + GTT verification
	@bash scripts/optimize/vram-gtt.sh

verify: ## Post-optimization verification checklist
	@bash scripts/optimize/verify.sh

rollback: ## Rollback optimizations
	@bash scripts/optimize/rollback.sh

# --- Agentic Evaluation ---
agentic-setup: ## Install agentic evaluation frameworks (inspect-ai, evalplus)
	@bash bin/agentic setup

agentic-quick: ## EvalPlus + IFEval quick eval (needs --model, ~1h)
	@bash bin/agentic quick $(ARGS)

agentic-code: ## Code generation eval: EvalPlus + BigCodeBench (~2-3h)
	@bash bin/agentic code $(ARGS)

agentic-tooluse: ## Tool/function calling eval: BFCL (~1-2h)
	@bash bin/agentic tooluse $(ARGS)

agentic-full: ## All agentic evaluations (~5-6h)
	@bash bin/agentic full $(ARGS)

# --- Tests ---
test: ## Run BATS test suite
	@bats tests/
