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 debug release run run-debug clean rebuild show-version pack controllerdb format format-check tidy tidy-fix cppcheck hooks-install help

# ==============================================================================
# COMPILACIÓ
# ==============================================================================
# Convenció alineada amb ../aee i ../projecte_2026:
#   make           → Release (binari d'ús normal)
#   make debug     → Debug (flux complet + `debug.yaml` per triar escena inicial)
#   make release   → Release + empaquetat per a distribució (TODO, pendent)
# ==============================================================================

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

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

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

run-debug: debug
	@./$(BUILDDIR)/$(PROJECT)

# Release + empaquetat per distribució (zip/dmg/tar.gz). De moment només
# compila amb CMAKE_BUILD_TYPE=Release; l'empaquetat OS-specific està
# pendent (veure Makefile de ../aee com a referència).
release: all
	@echo "TODO: empaquetat per a distribució (zip/dmg/tar.gz) — pendent"

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

clean:
	@rm -rf $(BUILDDIR)

rebuild: clean all

# Empaqueta data/ a $(BUILDDIR)/resources.pack (format AE26). Força un rebuild
# del pack encara que res no hagi canviat dins data/. L'empaquetat també es fa
# automàticament a cada `make`/`make debug` via el target CMake `resource_pack`.
pack:
	@cmake -S . -B $(BUILDDIR) -DCMAKE_BUILD_TYPE=Release $(CMAKE_DEFS)
	@cmake --build $(BUILDDIR) --target pack_resources
	@./$(BUILDDIR)/pack_resources data $(BUILDDIR)/resources.pack

# Descàrrega de la base de dades de mappings de mandos. SDL només pot
# obrir-la des del filesystem (no del pack), per això viu a data/input/.
controllerdb:
	@echo "Descarregant gamecontrollerdb.txt..."
	@curl -fsSL https://raw.githubusercontent.com/mdqinc/SDL_GameControllerDB/master/gamecontrollerdb.txt \
		-o data/input/gamecontrollerdb.txt
	@echo "data/input/gamecontrollerdb.txt actualitzat"

# ==============================================================================
# CODE QUALITY (delegados a cmake)
# ==============================================================================
format:
	@cmake -S . -B $(BUILDDIR) -DCMAKE_BUILD_TYPE=Release $(CMAKE_DEFS)
	@cmake --build $(BUILDDIR) --target format

format-check:
	@cmake -S . -B $(BUILDDIR) -DCMAKE_BUILD_TYPE=Release $(CMAKE_DEFS)
	@cmake --build $(BUILDDIR) --target format-check

tidy:
	@cmake -S . -B $(BUILDDIR) -DCMAKE_BUILD_TYPE=Release $(CMAKE_DEFS)
	@cmake --build $(BUILDDIR) --target tidy

tidy-fix:
	@cmake -S . -B $(BUILDDIR) -DCMAKE_BUILD_TYPE=Release $(CMAKE_DEFS)
	@cmake --build $(BUILDDIR) --target tidy-fix

cppcheck:
	@cmake -S . -B $(BUILDDIR) -DCMAKE_BUILD_TYPE=Release $(CMAKE_DEFS)
	@cmake --build $(BUILDDIR) --target cppcheck

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

# ==============================================================================
# AJUDA
# ==============================================================================
help:
	@echo "Makefile per a $(PROJECT)"
	@echo ""
	@echo "  Compilació:"
	@echo "    make                     - Compilar amb cmake (Release)"
	@echo "    make debug               - Compilar amb cmake (Debug)"
	@echo ""
	@echo "  Execució:"
	@echo "    make run                 - Compilar (Release) i executar"
	@echo "    make run-debug           - Compilar (Debug) i executar"
	@echo ""
	@echo "  Release:"
	@echo "    make release             - Release + empaquetat (TODO pendent)"
	@echo ""
	@echo "  Recursos:"
	@echo "    make pack                - Empaquetar data/ a $(BUILDDIR)/resources.pack (format AE26)"
	@echo "    make controllerdb        - Descarregar data/input/gamecontrollerdb.txt actualitzat"
	@echo ""
	@echo "  Qualitat de codi:"
	@echo "    make format              - Formatar codi amb clang-format"
	@echo "    make format-check        - Verificar format sense modificar"
	@echo "    make tidy                - Anàlisi estàtic amb clang-tidy"
	@echo "    make tidy-fix            - Anàlisi estàtic amb auto-fix"
	@echo "    make cppcheck            - Anàlisi estàtic amb cppcheck"
	@echo ""
	@echo "  Altres:"
	@echo "    make clean               - Esborrar carpeta $(BUILDDIR)/"
	@echo "    make rebuild             - clean + all"
	@echo "    make show-version        - Mostrar versió"
	@echo "    make hooks-install       - Activar git hooks del projecte"
	@echo ""
	@echo "  Versió actual: $(VERSION) ($(GIT_HASH))"
