# Makefile for Ballu AC ESP32-C6 Controller

.PHONY: all build test clean test-uart test-midea-protocol test-zigbee-zcl test-integration-layer 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
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
	@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) $(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) $(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) $(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) $(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

# 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 "  clean                - Clean build artifacts"
	@echo "  help                 - Show this help"