# Common tasks. Run `make help` for a list.
#
# Firmware commands run inside firmware/ (RISC-V target, build-std);
# everything else builds on the host.

PORT ?= $(shell ls /dev/cu.usbmodem* 2>/dev/null | head -1)
FW    = firmware/target/riscv32imac-unknown-none-elf/release/air-monitor
# Local time handed to the firmware so the first boot can set the RTC
# (firmware/src/clock.rs). Fresh on every make invocation.
export AIRMON_NOW := $(shell date +%s)
export AIRMON_TZ  := $(shell date +%z)

.PHONY: help run run-demo flash monitor build preview screenshots icons test lint fmt check clean

help: ## Show this help
	@grep -hE '^[a-z-]+:.*## ' $(MAKEFILE_LIST) | awk -F':.*## ' '{printf "  \033[36m%-12s\033[0m %s\n", $$1, $$2}'

run: ## Build, flash and open the serial monitor
	cd firmware && cargo run --release

run-demo: ## Same, with simulated sensors (no hardware needed)
	cd firmware && cargo run --release --features demo

flash: build ## Build and flash without monitoring
	espflash flash --chip esp32c6 $(if $(PORT),--port $(PORT)) $(FW)

monitor: ## Attach to the serial log (Ctrl-C to quit)
	espflash monitor --chip esp32c6 $(if $(PORT),--port $(PORT))

build: ## Build the firmware (release)
	cd firmware && cargo build --release

preview: ## Render all screens to target/preview/*.png
	cargo run -q -p airmon-preview --release

LUCIDE_VERSION ?= 1.66.0
LUCIDE_DIR := target/lucide-$(LUCIDE_VERSION)

icons: ## Re-bake Lucide icons (tools/icons) into airmon-ui
	@test -d $(LUCIDE_DIR) || (mkdir -p $(LUCIDE_DIR) && curl -sSfL https://registry.npmjs.org/lucide-static/-/lucide-static-$(LUCIDE_VERSION).tgz | tar xz -C $(LUCIDE_DIR))
	cargo run -q -p airmon-icons --release -- $(LUCIDE_DIR)/package/icons crates/airmon-ui/src/icons/lucide.rs $(LUCIDE_VERSION)

screenshots: ## Refresh docs/screenshots from the current UI
	cargo run -q -p airmon-preview --release -- docs/screenshots

test: ## Run host unit tests
	cargo test --workspace

lint: ## Clippy, host + firmware, warnings are errors
	cargo clippy --workspace --all-targets -- -D warnings
	cd firmware && cargo clippy --release -- -D warnings

fmt: ## Format everything
	cargo fmt --all
	cd firmware && cargo fmt

check: test lint ## Everything CI would run
	cargo fmt --all --check
	cd firmware && cargo fmt --check && cargo build --release

clean: ## Remove build artefacts
	cargo clean
	cd firmware && cargo clean
