# Build, flash and monitor the ESP32-S3 controller (ESP-IDF in Docker).
#
#   make              list the targets
#   make build        minify the web UI, then build the firmware
#   make flash        build if needed and flash the S3
#   make monitor      serial console (Ctrl-] to quit)
#   make flash-monitor
#
# Override on the command line, e.g.  make flash PORT=/dev/ttyACM1

PORT      ?= /dev/ttyACM0
IDF_IMAGE ?= espressif/idf:v5.5.5
SECS      ?= 10

DOCKER_RUN = docker run --rm -v "$(CURDIR)":/project -w /project
DEVICE     = --device=$(PORT)
# Interactive terminal only when there is one (monitor needs it, CI does not have it)
TTY       := $(shell [ -t 0 ] && echo -it)

# Rootless Docker maps container root to you, so files must end up owned by uid 0 inside; with a
# normal daemon they must be owned by you.
OWNER := $(shell docker info --format '{{.SecurityOptions}}' 2>/dev/null | grep -q rootless && echo 0:0 || echo $$(id -u):$$(id -g))
FIX_OWNER = chown -R $(OWNER) /project

.PHONY: help web build flash ota ota-drivers monitor flash-monitor log size menuconfig clean fullclean erase test test-driver test-envelope test-api test-browser

help:
	@echo "LED controller (ESP32-S3):"
	@echo "  make build           minify + gzip the web UI, then build the firmware"
	@echo "  make flash           flash firmware and web UI to PORT ($(PORT))"
	@echo "  make ota HOST=ip     build, then upload the firmware (web UI included) over Ethernet (no USB)"
	@echo "  make ota-drivers HOST=ip [MAC=aabbccddeeff] [BROADCAST=1]"
	@echo "                       push changed led-driver .py files over ESP-NOW (all drivers without MAC)"
	@echo "  make monitor         serial console on PORT (Ctrl-] to quit)"
	@echo "  make flash-monitor   flash, then open the console"
	@echo "  make log [SECS=10]   print the console for SECS seconds and exit (no terminal needed)"
	@echo "  make web             only rebuild the web UI (needs node/npx)"
	@echo "  make size            memory use of the last build"
	@echo "  make menuconfig      change the ESP-NOW channel, marker pin, W5500 pins, ..."
	@echo "  make erase           erase the whole flash (loses the stored data)"
	@echo "  make clean           remove the build directory (keeps sdkconfig)"
	@echo "  make fullclean       also remove sdkconfig and the web UI output"
	@echo "  make test            driver unit tests + C/Python envelope cross-check"
	@echo "  make test-api HOST=ip      HTTP API tests against a running controller (data restored afterwards)"
	@echo "  make test-browser HOST=ip  Selenium UI tests against a running controller (needs selenium + chromedriver)"
	@echo "variables: PORT=$(PORT)  IDF_IMAGE=$(IDF_IMAGE)"

# --- web UI ---------------------------------------------------------------
web:
	tools/build_web.sh

# --- firmware -------------------------------------------------------------
build: web
	$(DOCKER_RUN) $(IDF_IMAGE) bash -c 'idf.py build; rc=$$?; $(FIX_OWNER); exit $$rc'

flash: build
	$(DOCKER_RUN) $(DEVICE) $(IDF_IMAGE) bash -c 'idf.py -p $(PORT) flash; rc=$$?; $(FIX_OWNER); exit $$rc'

ota: build
	@test -n "$(HOST)" || { echo "usage: make ota HOST=<controller ip>"; exit 1; }
	python3 tools/ota.py $(HOST)

ota-drivers:
	@test -n "$(HOST)" || { echo "usage: make ota-drivers HOST=<controller ip> [MAC=...] [BROADCAST=1]"; exit 1; }
	python3 tools/driver_ota.py $(HOST) $(if $(MAC),--mac $(MAC),--all) $(if $(BROADCAST),--broadcast)

monitor:
	$(DOCKER_RUN) $(TTY) $(DEVICE) $(IDF_IMAGE) idf.py -p $(PORT) monitor

flash-monitor: flash monitor

# Non-interactive: reset the board and print what it says for SECS seconds
log:
	python3 tools/serial_log.py $(PORT) $(SECS)

size:
	$(DOCKER_RUN) $(IDF_IMAGE) bash -c 'idf.py size; rc=$$?; $(FIX_OWNER); exit $$rc'

menuconfig:
	$(DOCKER_RUN) -it $(IDF_IMAGE) bash -c 'idf.py menuconfig; rc=$$?; $(FIX_OWNER); exit $$rc'

erase:
	$(DOCKER_RUN) $(DEVICE) $(IDF_IMAGE) idf.py -p $(PORT) erase-flash

clean:
	rm -rf build

fullclean: clean
	rm -rf sdkconfig sdkconfig.old www_dist

# --- tests ----------------------------------------------------------------
test: test-driver test-envelope

# Host-side unit tests for the driver's link layer and beat sync (pure Python)
test-driver:
	python3 tools/run_driver_tests.py

# HTTP API tests (ported from v1) against a running controller; v1 features v2 lacks are xfail
test-api:
	@test -n "$(HOST)" || { echo "usage: make test-api HOST=<controller ip>"; exit 1; }
	python3 -m pytest test/api -q --host $(HOST)

# Selenium UI tests (ported from v1); they change the controller's data and do not restore it
test-browser:
	@test -n "$(HOST)" || { echo "usage: make test-browser HOST=<controller ip>"; exit 1; }
	LED_CONTROLLER_RUN_BROWSER_TESTS=1 LED_HOST=$(HOST) python3 -m pytest test/browser -q

# The C envelope packer must produce the same bytes as test/binary_envelope.py
test-envelope:
	@tmp=$$(mktemp -d); \
	(cd test && python3 env_check.py) > $$tmp/py.txt; \
	docker run --rm -v "$(CURDIR)":/project -w /project/test $(IDF_IMAGE) bash -c \
	  'gcc -Wall -I/opt/esp/idf/components/json/cJSON env_host.c ../main/envelope.c /opt/esp/idf/components/json/cJSON/cJSON.c -o /tmp/t -lm && /tmp/t' 2>&1 | tail -2 > $$tmp/c.txt; \
	if diff -q $$tmp/py.txt $$tmp/c.txt >/dev/null; then echo "envelope: C and Python agree"; rm -rf $$tmp; \
	else echo "envelope: MISMATCH"; diff $$tmp/py.txt $$tmp/c.txt; rm -rf $$tmp; exit 1; fi
