# ------------------------------------------------------------------------------
# Compiler and Base Flags
# ------------------------------------------------------------------------------
CXX := g++
CXXFLAGS := -Wall -Wextra -std=c++17 -g -DDEBUG -DTIMERS

# ------------------------------------------------------------------------------
# Detect Platform
# ------------------------------------------------------------------------------
ifeq ($(OS),Windows_NT)
    # -------------------- Windows --------------------
    EXE_EXT := .exe

    # Example paths (adjust if you have your libs somewhere else):
    # Include directories for GLFW, GLEW, etc.
    GLFW_INCLUDE := C:/libs/glfw/include
    GLEW_INCLUDE := C:/libs/glew/include

    # Library search paths
    LIB_PATHS := -LC:/libs/glfw/lib \
                 -LC:/libs/glew/lib \
                 -Llib

    # Libraries to link against (adjust if needed)
    LIBS := -lglfw3 -lopengl32 -lgdi32 -limm32 -lole32 -loleaut32 -luuid \
            -lwinmm -lglew32 -lglu32 -lyaml-cpp -llua54

    # A Windows-compatible "mkdir" command:
    define MKDIR_P
        if not exist "$(1)" mkdir "$(1)" >nul 2>&1
    endef

else
    # -------------------- Linux (or other Unix-likes) --------------------
    EXE_EXT :=

    # If libs are installed system-wide, you usually only need -lGL, -lGLEW, etc.
    # But you can still point to custom paths if needed:
    GLFW_INCLUDE := /usr/include
    GLEW_INCLUDE := /usr/include

    # Library search paths (often not needed if system-wide)
    LIB_PATHS :=

    # Libraries on Linux
    LIBS := -lglfw -lGL -lGLEW -lGLU -lyaml-cpp -llua

    # A Linux-compatible "mkdir -p" command:
    define MKDIR_P
        mkdir -p "$(1)"
    endef

endif

# ------------------------------------------------------------------------------
# Directories
# ------------------------------------------------------------------------------
SRC_DIR := src
VENDOR_DIRS := vendor/imgui-docking vendor/stb vendor/ImGuizmo
BUILD_DIR := build

# ------------------------------------------------------------------------------
# Include Directories
# ------------------------------------------------------------------------------
INCLUDE_DIRS := \
    $(SRC_DIR) \
    $(VENDOR_DIRS) \
    $(GLFW_INCLUDE) \
    $(GLEW_INCLUDE) \
    vendor/stb/include \
    vendor/lua \
    vendor/ImGuizmo \
    vendor/gcml

# Add them to compiler flags:
CXXFLAGS += $(foreach inc,$(INCLUDE_DIRS),-I$(inc))

# ------------------------------------------------------------------------------
# Source Files
# ------------------------------------------------------------------------------
SRC_FILES := $(wildcard $(SRC_DIR)/*.cpp) \
             $(wildcard $(SRC_DIR)/**/*.cpp)

VENDOR_SRC := $(foreach dir, $(VENDOR_DIRS), $(wildcard $(dir)/*.cpp))
STB_SRC := $(wildcard vendor/stb/src/*.cpp) # If stb has .cpp files
ALL_SRC := $(SRC_FILES) $(VENDOR_SRC) $(STB_SRC)

# ------------------------------------------------------------------------------
# Object Files
# ------------------------------------------------------------------------------
OBJ_FILES := $(patsubst %.cpp, $(BUILD_DIR)/%.o, $(ALL_SRC))

# ------------------------------------------------------------------------------
# Target
# ------------------------------------------------------------------------------
TARGET := TesseractEngine$(EXE_EXT)

# ------------------------------------------------------------------------------
# Phony Targets
# ------------------------------------------------------------------------------
.PHONY: all clean

# Default target
all: $(TARGET)

# ------------------------------------------------------------------------------
# Link all object files to create the executable
# ------------------------------------------------------------------------------
$(TARGET): $(OBJ_FILES)
	@echo Linking $@...
	$(CXX) $(CXXFLAGS) -o $@ $^ $(LIB_PATHS) $(LIBS)

# ------------------------------------------------------------------------------
# Pattern rule to compile .cpp files to .o files
# ------------------------------------------------------------------------------
$(BUILD_DIR)/%.o: %.cpp
	@$(call MKDIR_P,$(dir $@))
	@echo Compiling $<...
	$(CXX) $(CXXFLAGS) -c $< -o $@

# ------------------------------------------------------------------------------
# Clean build artifacts
# ------------------------------------------------------------------------------
clean:
	@echo Cleaning up...
	rm -rf "$(BUILD_DIR)"
	rm -f "$(TARGET)"