PROJECT  := aee_2026
BUILDDIR := build
JOBS     ?= $(shell nproc 2>/dev/null || echo 4)

# VERSION (única font de veritat: source/utils/defines.hpp)
VERSION  := v$(shell grep 'constexpr const char\* VERSION' source/utils/defines.hpp | sed -E 's/.*VERSION = "([^"]+)".*/\1/')

# GIT_HASH calculat al host i passat a CMake (evita problemes si CMake corre en
# entorns sense accés a git, p.ex. Docker/emscripten).
GIT_HASH := $(shell git rev-parse --short=7 HEAD 2>/dev/null)
ifeq ($(GIT_HASH),)
    GIT_HASH := unknown
endif

CMAKE_DEFS := -DGIT_HASH=$(GIT_HASH)

.PHONY: all configure build run clean rebuild release show-version format format-check tidy tidy-fix cppcheck hooks-install

all: build

configure:
	@cmake -S . -B $(BUILDDIR) -DCMAKE_BUILD_TYPE=Debug $(CMAKE_DEFS)

build: configure
	@cmake --build $(BUILDDIR) -j$(JOBS)

run: build
	@./$(BUILDDIR)/$(PROJECT)

release:
	@cmake -S . -B $(BUILDDIR) -DCMAKE_BUILD_TYPE=Release $(CMAKE_DEFS)
	@cmake --build $(BUILDDIR) -j$(JOBS)

show-version:
	@echo "$(PROJECT) $(VERSION) ($(GIT_HASH))"

clean:
	@rm -rf $(BUILDDIR)

rebuild: clean build

# ==============================================================================
# CODE QUALITY (delegados a cmake)
# ==============================================================================
format: configure
	@cmake --build $(BUILDDIR) --target format

format-check: configure
	@cmake --build $(BUILDDIR) --target format-check

tidy: configure
	@cmake --build $(BUILDDIR) --target tidy

tidy-fix: configure
	@cmake --build $(BUILDDIR) --target tidy-fix

cppcheck: configure
	@cmake --build $(BUILDDIR) --target cppcheck

# ==============================================================================
# GIT HOOKS
# ==============================================================================
hooks-install:
	@git config core.hooksPath .githooks
	@echo "Git hooks activats: $(shell pwd)/.githooks"
