# CFlang Makefile
CC = gcc
CFLAGS = -Wall -Wextra -O3 -std=c99 -march=native -flto -ffast-math -fno-math-errno -fno-trapping-math -funroll-loops
DEBUG_CFLAGS = -Wall -Wextra -O0 -g -std=c99 -DDEBUG
LIBS = -lm

# Directories
SRC_DIR = ../app/src/main/cpp
INCLUDE_DIR = ../app/src/main/cpp/include
BUILD_DIR = build
TEST_DIR = tests

# Source files
SOURCES = $(SRC_DIR)/fast_board.c $(SRC_DIR)/fast_move_generator.c $(SRC_DIR)/fast_evaluation.c $(SRC_DIR)/fast_bot.c $(SRC_DIR)/fast_zobrist.c $(SRC_DIR)/fast_transposition.c $(SRC_DIR)/bitboard_utils.c
MAIN_SOURCE = $(SRC_DIR)/main.c
OBJECTS = $(SOURCES:$(SRC_DIR)/%.c=$(BUILD_DIR)/%.o)
MAIN_OBJECT = $(BUILD_DIR)/main.o

# Targets
TARGET = cflang
DEBUG_TARGET = cflang_debug

.PHONY: all clean debug test install

# Default target
all: $(TARGET)

# Release build
$(TARGET): $(BUILD_DIR) $(OBJECTS) $(MAIN_OBJECT)
	$(CC) $(CFLAGS) $(OBJECTS) $(MAIN_OBJECT) -o $(TARGET) $(LIBS)
	@echo "Built $(TARGET) successfully!"

# Debug build
debug: CFLAGS = $(DEBUG_CFLAGS)
debug: $(DEBUG_TARGET)

$(DEBUG_TARGET): $(BUILD_DIR) $(OBJECTS) $(MAIN_OBJECT)
	$(CC) $(DEBUG_CFLAGS) $(OBJECTS) $(MAIN_OBJECT) -o $(DEBUG_TARGET) $(LIBS)
	@echo "Built $(DEBUG_TARGET) successfully!"

# Create build directory
$(BUILD_DIR):
	mkdir -p $(BUILD_DIR)

# Compile source files
$(BUILD_DIR)/%.o: $(SRC_DIR)/%.c
	$(CC) $(CFLAGS) -I$(INCLUDE_DIR) -c $< -o $@

# Clean build artifacts
clean:
	rm -rf $(BUILD_DIR)
	rm -f $(TARGET) $(DEBUG_TARGET)
	@echo "Cleaned build artifacts"

# Run the program
run: $(TARGET)
	./$(TARGET)

# Run debug version
run-debug: debug
	./$(DEBUG_TARGET)

# Install (copy to /usr/local/bin)
install: $(TARGET)
	sudo cp $(TARGET) /usr/local/bin/
	@echo "Installed $(TARGET) to /usr/local/bin/"

# Uninstall
uninstall:
	sudo rm -f /usr/local/bin/$(TARGET)
	@echo "Uninstalled $(TARGET)"

# Performance test
perf: $(TARGET)
	time ./$(TARGET)

# Memory check (requires valgrind)
memcheck: debug
	valgrind --leak-check=full --show-leak-kinds=all ./$(DEBUG_TARGET)

# Static analysis (requires cppcheck)
analyze:
	cppcheck --enable=all --inconclusive --std=c99 -I$(INCLUDE_DIR) $(SRC_DIR)/*.c

# Show help
help:
	@echo "CFlang Build System"
	@echo "==================="
	@echo ""
	@echo "Available targets:"
	@echo "  all        - Build release version (default)"
	@echo "  debug      - Build debug version with symbols"
	@echo "  clean      - Remove build artifacts"
	@echo "  run        - Build and run release version"
	@echo "  run-debug  - Build and run debug version"
	@echo "  install    - Install to /usr/local/bin"
	@echo "  uninstall  - Remove from /usr/local/bin"
	@echo "  perf       - Run performance test"
	@echo "  memcheck   - Run memory leak check (requires valgrind)"
	@echo "  analyze    - Run static analysis (requires cppcheck)"
	@echo "  help       - Show this help message"
	@echo ""
	@echo "Build options:"
	@echo "  CC         - C compiler (default: gcc)"
	@echo "  CFLAGS     - Compiler flags"
	@echo "  LIBS       - Libraries to link"
	@echo ""
	@echo "Examples:"
	@echo "  make                    # Build release version"
	@echo "  make debug              # Build debug version"
	@echo "  make CC=clang           # Build with clang"
	@echo "  make CFLAGS='-O2 -g'    # Custom compiler flags"