# Makefile for Ballu AC ESP32-C6 Controller

.PHONY: all build test clean test-uart test-midea-protocol test-zigbee-zcl test-integration-layer test-status-monitor test-app-controller help

# Directories
SRC_DIR := src
TEST_DIR := test
BUILD_DIR := build
UNITY_DIR := unity

# Source files
SRC_FILES := $(wildcard $(SRC_DIR)/*.c)
TEST_FILES := $(wildcard $(TEST_DIR)/*_test.c)

# Compiler settings
CC := gcc
CFLAGS := -Wall -Wextra -std=c99 -I$(SRC_DIR) -I$(UNITY_DIR)/src
# Test builds link every src/*.c together, so main()'s entry point must be
# excluded to avoid clashing with each test's own main().
TEST_CFLAGS := $(CFLAGS) -DUNIT_TEST
LDFLAGS :=

# Unity test framework
UNITY_SRC := $(UNITY_DIR)/src/unity.c

# Default target
all: build test

# Build the main application
build: $(BUILD_DIR)/app

$(BUILD_DIR)/app: $(SRC_FILES)
	@mkdir -p $(BUILD_DIR)
	$(CC) $(CFLAGS) $^ -o $@ $(LDFLAGS)

# Build and run tests
test: test-uart test-midea-protocol test-zigbee-zcl test-integration-layer test-status-monitor test-app-controller
	@echo "All tests passed!"

# Build individual test executables
$(TEST_DIR)/uart_driver_test: $(TEST_DIR)/uart_driver_test.c $(SRC_FILES) $(UNITY_SRC)
	@mkdir -p $(BUILD_DIR)
	$(CC) $(TEST_CFLAGS) $< $(SRC_FILES) $(UNITY_SRC) -o $@ $(LDFLAGS)

$(TEST_DIR)/midea_protocol_test: $(TEST_DIR)/midea_protocol_test.c $(SRC_FILES) $(UNITY_SRC)
	@mkdir -p $(BUILD_DIR)
	$(CC) $(TEST_CFLAGS) $< $(SRC_FILES) $(UNITY_SRC) -o $@ $(LDFLAGS)

$(TEST_DIR)/zigbee_zcl_test: $(TEST_DIR)/zigbee_zcl_test.c $(SRC_FILES) $(UNITY_SRC)
	@mkdir -p $(BUILD_DIR)
	$(CC) $(TEST_CFLAGS) $< $(SRC_FILES) $(UNITY_SRC) -o $@ $(LDFLAGS)

$(TEST_DIR)/integration_layer_test: $(TEST_DIR)/integration_layer_test.c $(SRC_FILES) $(UNITY_SRC)
	@mkdir -p $(BUILD_DIR)
	$(CC) $(TEST_CFLAGS) $< $(SRC_FILES) $(UNITY_SRC) -o $@ $(LDFLAGS)

$(TEST_DIR)/status_monitor_test: $(TEST_DIR)/status_monitor_test.c $(SRC_FILES) $(UNITY_SRC)
	@mkdir -p $(BUILD_DIR)
	$(CC) $(TEST_CFLAGS) $< $(SRC_FILES) $(UNITY_SRC) -o $@ $(LDFLAGS)

$(TEST_DIR)/app_controller_test: $(TEST_DIR)/app_controller_test.c $(SRC_FILES) $(UNITY_SRC)
	@mkdir -p $(BUILD_DIR)
	$(CC) $(TEST_CFLAGS) $< $(SRC_FILES) $(UNITY_SRC) -o $@ $(LDFLAGS)

# Run individual tests
test-uart: $(TEST_DIR)/uart_driver_test
	@echo "Running UART driver tests..."
	@./$(TEST_DIR)/uart_driver_test

test-midea-protocol: $(TEST_DIR)/midea_protocol_test
	@echo "Running Midea protocol tests..."
	@./$(TEST_DIR)/midea_protocol_test

test-zigbee-zcl: $(TEST_DIR)/zigbee_zcl_test
	@echo "Running Zigbee ZCL tests..."
	@./$(TEST_DIR)/zigbee_zcl_test

test-integration-layer: $(TEST_DIR)/integration_layer_test
	@echo "Running integration layer tests..."
	@./$(TEST_DIR)/integration_layer_test

test-status-monitor: $(TEST_DIR)/status_monitor_test
	@echo "Running status monitor tests..."
	@./$(TEST_DIR)/status_monitor_test

test-app-controller: $(TEST_DIR)/app_controller_test
	@echo "Running app controller (end-to-end) tests..."
	@./$(TEST_DIR)/app_controller_test

# Clean build artifacts
clean:
	rm -rf $(BUILD_DIR)
	rm -f $(TEST_DIR)/*_test

# Help
help:
	@echo "Available targets:"
	@echo "  all                  - Build and run tests"
	@echo "  build                - Build the application"
	@echo "  test                 - Run all tests"
	@echo "  test-uart            - Run UART driver tests"
	@echo "  test-midea-protocol  - Run Midea protocol tests"
	@echo "  test-zigbee-zcl      - Run Zigbee ZCL tests"
	@echo "  test-integration-layer - Run integration layer tests"
	@echo "  test-status-monitor  - Run status monitor tests"
	@echo "  test-app-controller  - Run end-to-end app controller tests"
	@echo "  clean                - Clean build artifacts"
	@echo "  help                 - Show this help"