# ==============================================================================
# DIRECTORIES
# ==============================================================================
DIR_ROOT       := $(dir $(abspath $(MAKEFILE_LIST)))
DIR_SOURCES    := $(addsuffix /, $(DIR_ROOT)source)
DIR_BIN        := $(addsuffix /, $(DIR_ROOT))
DIR_TOOLS      := $(addsuffix /, $(DIR_ROOT)tools)

# ==============================================================================
# TARGET NAMES (extraídos de CMakeLists.txt)
# ==============================================================================
ifeq ($(OS),Windows_NT)
    TARGET_NAME    := $(shell powershell -Command "$$line = Get-Content CMakeLists.txt | Where-Object {$$_ -match '^project'}; if ($$line -match 'project\s*\x28(\w+)') { $$matches[1] }")
    LONG_NAME      := $(shell powershell -Command "$$line = Get-Content CMakeLists.txt | Where-Object {$$_ -match 'PROJECT_LONG_NAME'}; if ($$line -match '\"(.+)\"') { $$matches[1] }")
else
    TARGET_NAME    := $(shell awk '/^project/ {gsub(/[)(]/, " "); print $$2}' CMakeLists.txt)
    LONG_NAME      := $(shell grep 'PROJECT_LONG_NAME' CMakeLists.txt | sed 's/.*"\(.*\)".*/\1/')
endif
TARGET_FILE    := $(DIR_BIN)$(TARGET_NAME)
APP_NAME       := $(LONG_NAME)
RELEASE_FOLDER := $(TARGET_NAME)_release
RELEASE_FILE   := $(RELEASE_FOLDER)/$(TARGET_NAME)
RESOURCE_FILE  := release/$(TARGET_NAME).res

# ==============================================================================
# PACKING TOOL
# ==============================================================================
ifeq ($(OS),Windows_NT)
    PACK_TOOL      := $(DIR_TOOLS)pack_resources/pack_resources.exe
    PACK_CXX       := $(CXX)
else
    PACK_TOOL      := $(DIR_TOOLS)pack_resources/pack_resources
    PACK_CXX       := $(CXX)
endif
PACK_SOURCES   := $(DIR_TOOLS)pack_resources/pack_resources.cpp source/core/resources/resource_pack.cpp
PACK_INCLUDES  := -Isource

# ==============================================================================
# VERSION (extraída de CMakeLists.txt)
# ==============================================================================
ifeq ($(OS),Windows_NT)
    VERSION := v$(shell powershell -Command "$$line = Get-Content CMakeLists.txt | Where-Object {$$_ -match '^project'}; if ($$line -match 'VERSION\s+([0-9.]+)') { $$matches[1] }")
else
    VERSION := v$(shell grep "^project" CMakeLists.txt | tr -cd 0-9.)
endif

# ==============================================================================
# WINDOWS-SPECIFIC VARIABLES
# ==============================================================================
ifeq ($(OS),Windows_NT)
    WIN_TARGET_FILE    := $(DIR_BIN)$(APP_NAME)
    WIN_RELEASE_FILE   := $(RELEASE_FOLDER)/$(APP_NAME)
else
    WIN_TARGET_FILE    := $(TARGET_FILE)
    WIN_RELEASE_FILE   := $(RELEASE_FILE)
endif

# ==============================================================================
# RELEASE NAMES
# ==============================================================================
WINDOWS_RELEASE             := $(TARGET_NAME)-$(VERSION)-win32-x64.zip
MACOS_INTEL_RELEASE         := $(TARGET_NAME)-$(VERSION)-macos-intel.dmg
MACOS_APPLE_SILICON_RELEASE := $(TARGET_NAME)-$(VERSION)-macos-apple-silicon.dmg
LINUX_RELEASE               := $(TARGET_NAME)-$(VERSION)-linux.tar.gz
RPI_RELEASE                 := $(TARGET_NAME)-$(VERSION)-rpi-arm64.tar.gz

# ==============================================================================
# SOURCE FILES
# ==============================================================================
APP_SOURCES := \
    source/main.cpp \
    source/core/audio/audio.cpp \
    source/core/input/input.cpp \
    source/core/input/input_types.cpp \
    source/core/input/mouse.cpp \
    source/core/input/global_inputs.cpp \
    source/core/rendering/screen.cpp \
    source/core/rendering/surface.cpp \
    source/core/rendering/surface_sprite.cpp \
    source/core/rendering/surface_animated_sprite.cpp \
    source/core/rendering/surface_moving_sprite.cpp \
    source/core/rendering/text.cpp \
    source/core/rendering/texture.cpp \
    source/core/rendering/gif.cpp \
    source/core/rendering/opengl/opengl_shader.cpp \
    source/core/resources/resource_list.cpp \
    source/core/resources/resource_cache.cpp \
    source/core/resources/resource_helper.cpp \
    source/core/resources/resource_loader.cpp \
    source/core/resources/resource_pack.cpp \
    source/core/system/director.cpp \
    source/core/system/debug.cpp \
    source/core/system/global_events.cpp \
    source/game/options.cpp \
    source/game/entities/player.cpp \
    source/game/entities/enemy.cpp \
    source/game/entities/item.cpp \
    source/game/gameplay/room.cpp \
    source/game/gameplay/collision_map.cpp \
    source/game/gameplay/enemy_manager.cpp \
    source/game/gameplay/item_manager.cpp \
    source/game/gameplay/room_loader.cpp \
    source/game/gameplay/tilemap_renderer.cpp \
    source/game/gameplay/scoreboard.cpp \
    source/game/gameplay/item_tracker.cpp \
    source/game/gameplay/room_tracker.cpp \
    source/game/scenes/logo.cpp \
    source/game/scenes/title.cpp \
    source/game/scenes/game.cpp \
    source/game/ui/notifier.cpp \
    source/utils/utils.cpp \
    source/utils/color.cpp \
    source/utils/delta_timer.cpp

# All sources combined
ALL_SOURCES := $(APP_SOURCES)

# ==============================================================================
# INCLUDES
# ==============================================================================
INCLUDES := -Isource

# ==============================================================================
# COMPILER FLAGS (OS-specific)
# ==============================================================================
CPP_STANDARD := c++20

ifeq ($(OS),Windows_NT)
    FixPath         = $(subst /,\\,$1)
    CXXFLAGS        := -std=$(CPP_STANDARD) -Wall -Os -ffunction-sections -fdata-sections \
                       -Wl,--gc-sections -static-libstdc++ -static-libgcc \
                       -Wl,-subsystem,windows -DWINDOWS_BUILD
    CXXFLAGS_DEBUG  := -std=$(CPP_STANDARD) -Wall -g -D_DEBUG -DWINDOWS_BUILD
    LDFLAGS         := -lmingw32 -lws2_32 -lSDL3 -lopengl32
    RM              := del /Q
    MKDIR           := mkdir
else
    FixPath         = $1
    CXXFLAGS        := -std=$(CPP_STANDARD) -Wall -Os -ffunction-sections -fdata-sections
    CXXFLAGS_DEBUG  := -std=$(CPP_STANDARD) -Wall -g -D_DEBUG
    LDFLAGS         := -lSDL3
    RMFILE          := rm -f
    RMDIR           := rm -rdf
    MKDIR           := mkdir -p
    UNAME_S         := $(shell uname -s)
    ifeq ($(UNAME_S),Linux)
        CXXFLAGS    += -DLINUX_BUILD
        LDFLAGS     += -lGL
    endif
    ifeq ($(UNAME_S),Darwin)
        CXXFLAGS       += -Wno-deprecated -DMACOS_BUILD
        CXXFLAGS_DEBUG += -Wno-deprecated -DMACOS_BUILD
        LDFLAGS        += -framework OpenGL
        # Configurar arquitectura (por defecto arm64)
        CXXFLAGS       += -arch arm64
        CXXFLAGS_DEBUG += -arch arm64
    endif
endif

# ==============================================================================
# REGLAS PARA HERRAMIENTA DE EMPAQUETADO Y RESOURCES.PACK
# ==============================================================================
$(PACK_TOOL): FORCE
	@echo "Compilando herramienta de empaquetado..."
	$(PACK_CXX) -std=$(CPP_STANDARD) -Wall -Os $(PACK_INCLUDES) $(PACK_SOURCES) -o $(PACK_TOOL)
	@echo "✓ Herramienta de empaquetado lista: $(PACK_TOOL)"

pack_tool: $(PACK_TOOL)

resources.pack: $(PACK_TOOL)
	@echo "Generando resources.pack desde directorio data/..."
	$(PACK_TOOL) data resources.pack
	@echo "✓ resources.pack generado exitosamente"

# ==============================================================================
# COMPILACIÓN PARA WINDOWS
# ==============================================================================
windows:
	@echo off
	@echo Compilando para Windows con nombre: "$(WIN_TARGET_FILE).exe"
	windres release/$(TARGET_NAME).rc -O coff -o $(RESOURCE_FILE)
	g++ $(ALL_SOURCES) $(RESOURCE_FILE) $(INCLUDES) $(CXXFLAGS) $(LDFLAGS) -o "$(WIN_TARGET_FILE).exe"
	strip -s -R .comment -R .gnu.version "$(WIN_TARGET_FILE).exe" --strip-unneeded

windows_debug:
	@echo off
	@echo Compilando version debug para Windows: "$(WIN_TARGET_FILE)_debug.exe"
	windres release/$(TARGET_NAME).rc -O coff -o $(RESOURCE_FILE)
	g++ $(ALL_SOURCES) $(RESOURCE_FILE) $(INCLUDES) -DDEBUG -DVERBOSE $(CXXFLAGS_DEBUG) $(LDFLAGS) -o "$(WIN_TARGET_FILE)_debug.exe"

windows_release:
	@$(MAKE) pack_tool
	@$(MAKE) resources.pack
	@echo off
	@echo Creando release para Windows - Version: $(VERSION)

# Generate project.h from project.h.in
	@echo "Generando project.h..."
	@powershell -Command "$$GIT_HASH = (git rev-parse --short=7 HEAD 2>$$null); if (-not $$GIT_HASH) { $$GIT_HASH = 'unknown' }; $$RAW_VERSION = '$(VERSION)' -replace '^v', ''; $$COPYRIGHT = (Select-String -Path 'CMakeLists.txt' -Pattern 'set\(PROJECT_COPYRIGHT \"(.+?)\"').Matches.Groups[1].Value; (Get-Content source/project.h.in) -replace '@PROJECT_NAME@', '$(TARGET_NAME)' -replace '@PROJECT_LONG_NAME@', '$(LONG_NAME)' -replace '@PROJECT_VERSION@', $$RAW_VERSION -replace '@PROJECT_COPYRIGHT@', $$COPYRIGHT -replace '@GIT_HASH@', $$GIT_HASH | Set-Content source/project.h"

# Crea carpeta temporal 'RELEASE_FOLDER'
	powershell if (Test-Path "$(RELEASE_FOLDER)") {Remove-Item "$(RELEASE_FOLDER)" -Recurse -Force}
	powershell if (-not (Test-Path "$(RELEASE_FOLDER)")) {New-Item "$(RELEASE_FOLDER)" -ItemType Directory}

# Copia el archivo 'resources.pack'
	powershell Copy-Item -Path "resources.pack" -Destination "$(RELEASE_FOLDER)"

# Copia los ficheros que están en la raíz del proyecto
	powershell Copy-Item "LICENSE" -Destination "$(RELEASE_FOLDER)"
	powershell Copy-Item "README.md" -Destination "$(RELEASE_FOLDER)"
	powershell Copy-Item "gamecontrollerdb.txt" -Destination "$(RELEASE_FOLDER)"
	powershell Copy-Item "release\dll\*.dll" -Destination "$(RELEASE_FOLDER)"

# Compila (con icono)
	windres release/$(TARGET_NAME).rc -O coff -o $(RESOURCE_FILE)
	g++ $(ALL_SOURCES) $(RESOURCE_FILE) $(INCLUDES) -DRELEASE_BUILD $(CXXFLAGS) $(LDFLAGS) -o "$(WIN_RELEASE_FILE).exe"
	strip -s -R .comment -R .gnu.version "$(WIN_RELEASE_FILE).exe" --strip-unneeded

# Crea el fichero .zip
	powershell if (Test-Path "$(WINDOWS_RELEASE)") {Remove-Item "$(WINDOWS_RELEASE)"}
	powershell Compress-Archive -Path "$(RELEASE_FOLDER)"/* -DestinationPath "$(WINDOWS_RELEASE)"
	@echo Release creado: $(WINDOWS_RELEASE)

# Elimina la carpeta temporal 'RELEASE_FOLDER'
	powershell if (Test-Path "$(RELEASE_FOLDER)") {Remove-Item "$(RELEASE_FOLDER)" -Recurse -Force}

# ==============================================================================
# COMPILACIÓN PARA MACOS
# ==============================================================================
macos:
	@echo "Compilando para macOS: $(TARGET_NAME)"
	clang++ $(ALL_SOURCES) $(INCLUDES) $(CXXFLAGS) $(LDFLAGS) -o "$(TARGET_FILE)"

macos_debug:
	@echo "Compilando version debug para macOS: $(TARGET_NAME)_debug"
	clang++ $(ALL_SOURCES) $(INCLUDES) -DDEBUG -DVERBOSE $(CXXFLAGS_DEBUG) $(LDFLAGS) -o "$(TARGET_FILE)_debug"

macos_release:
	@$(MAKE) pack_tool
	@$(MAKE) resources.pack
	@echo "Creando release para macOS - Version: $(VERSION)"

# Verificar e instalar create-dmg si es necesario
	@which create-dmg > /dev/null || (echo "Instalando create-dmg..." && brew install create-dmg)

# Generate project.h from project.h.in
	@echo "Generando project.h..."
	@GIT_HASH=$$(git rev-parse --short=7 HEAD 2>/dev/null || echo "unknown"); \
	RAW_VERSION=$$(echo "$(VERSION)" | sed 's/^v//'); \
	COPYRIGHT=$$(sed -n 's/.*set(PROJECT_COPYRIGHT "\([^"]*\)".*/\1/p' CMakeLists.txt); \
	sed -e "s/@PROJECT_NAME@/$(TARGET_NAME)/g" \
	    -e "s/@PROJECT_LONG_NAME@/$(LONG_NAME)/g" \
	    -e "s/@PROJECT_VERSION@/$$RAW_VERSION/g" \
	    -e "s/@PROJECT_COPYRIGHT@/$$COPYRIGHT/g" \
	    -e "s/@GIT_HASH@/$$GIT_HASH/g" \
	    source/project.h.in > source/project.h

# Elimina datos de compilaciones anteriores
	$(RMDIR) "$(RELEASE_FOLDER)"
	$(RMDIR) Frameworks
	$(RMFILE) tmp.dmg
	$(RMFILE) "$(MACOS_INTEL_RELEASE)"
	$(RMFILE) "$(MACOS_APPLE_SILICON_RELEASE)"

# Crea la carpeta temporal para hacer el trabajo y las carpetas obligatorias para crear una app de macOS
	$(MKDIR) "$(RELEASE_FOLDER)/$(APP_NAME).app/Contents/Frameworks"
	$(MKDIR) "$(RELEASE_FOLDER)/$(APP_NAME).app/Contents/MacOS"
	$(MKDIR) "$(RELEASE_FOLDER)/$(APP_NAME).app/Contents/Resources"
	$(MKDIR) Frameworks

# Copia carpetas y ficheros
	cp resources.pack "$(RELEASE_FOLDER)/$(APP_NAME).app/Contents/Resources"
	cp gamecontrollerdb.txt "$(RELEASE_FOLDER)/$(APP_NAME).app/Contents/Resources"
	ditto release/frameworks/SDL3.xcframework/macos-arm64_x86_64/SDL3.framework "$(RELEASE_FOLDER)/$(APP_NAME).app/Contents/Frameworks/SDL3.framework"
	ditto release/frameworks/SDL3.xcframework/macos-arm64_x86_64/SDL3.framework Frameworks/SDL3.framework
# Recrear symlinks del framework (pueden estar rotos como archivos de texto)
	cd Frameworks/SDL3.framework && rm -f SDL3 Headers Resources && ln -s Versions/Current/SDL3 SDL3 && ln -s Versions/Current/Headers Headers && ln -s Versions/Current/Resources Resources
	cd Frameworks/SDL3.framework/Versions && rm -f Current && ln -s A Current
	cd "$(RELEASE_FOLDER)/$(APP_NAME).app/Contents/Frameworks/SDL3.framework" && rm -f SDL3 Headers Resources && ln -s Versions/Current/SDL3 SDL3 && ln -s Versions/Current/Headers Headers && ln -s Versions/Current/Resources Resources
	cd "$(RELEASE_FOLDER)/$(APP_NAME).app/Contents/Frameworks/SDL3.framework/Versions" && rm -f Current && ln -s A Current
	cp release/*.icns "$(RELEASE_FOLDER)/$(APP_NAME).app/Contents/Resources"
	cp release/Info.plist "$(RELEASE_FOLDER)/$(APP_NAME).app/Contents"
	cp LICENSE "$(RELEASE_FOLDER)"
	cp README.md "$(RELEASE_FOLDER)"

# Actualiza versión en Info.plist
	@echo "Actualizando Info.plist con versión $(VERSION)..."
	@RAW_VERSION=$$(echo "$(VERSION)" | sed 's/^v//'); \
	sed -i '' '/<key>CFBundleShortVersionString<\/key>/{n;s|<string>.*</string>|<string>'"$$RAW_VERSION"'</string>|;}' "$(RELEASE_FOLDER)/$(APP_NAME).app/Contents/Info.plist"; \
	sed -i '' '/<key>CFBundleVersion<\/key>/{n;s|<string>.*</string>|<string>'"$$RAW_VERSION"'</string>|;}' "$(RELEASE_FOLDER)/$(APP_NAME).app/Contents/Info.plist"

# Compila la versión para procesadores Intel
	clang++ $(ALL_SOURCES) $(INCLUDES) -DMACOS_BUNDLE -DRELEASE_BUILD -std=$(CPP_STANDARD) -Wall -Os -framework SDL3 -F ./Frameworks -framework OpenGL -Wno-deprecated -ffunction-sections -fdata-sections -o "$(RELEASE_FOLDER)/$(APP_NAME).app/Contents/MacOS/$(TARGET_NAME)" -rpath @executable_path/../Frameworks/ -target x86_64-apple-macos13.3 -mmacosx-version-min=13.3 -D_LIBCPP_DISABLE_AVAILABILITY

# Firma la aplicación
	codesign --deep --force --sign - --timestamp=none "$(RELEASE_FOLDER)/$(APP_NAME).app"

# Empaqueta el .dmg de la versión Intel con create-dmg
	@echo "Creando DMG Intel con iconos de 96x96..."
	create-dmg \
	  --volname "$(APP_NAME)" \
	  --window-pos 200 120 \
	  --window-size 720 300 \
	  --icon-size 96 \
	  --text-size 12 \
	  --icon "$(APP_NAME).app" 278 102 \
	  --icon "LICENSE" 441 102 \
	  --icon "README.md" 604 102 \
	  --app-drop-link 115 102 \
	  --hide-extension "$(APP_NAME).app" \
	  "$(MACOS_INTEL_RELEASE)" \
	  "$(RELEASE_FOLDER)" || true
	@echo "Release Intel creado: $(MACOS_INTEL_RELEASE)"

# Compila la versión para procesadores Apple Silicon
	clang++ $(ALL_SOURCES) $(INCLUDES) -DMACOS_BUNDLE -DRELEASE_BUILD -std=$(CPP_STANDARD) -Wall -Os -framework SDL3 -F ./Frameworks -framework OpenGL -Wno-deprecated -ffunction-sections -fdata-sections -o "$(RELEASE_FOLDER)/$(APP_NAME).app/Contents/MacOS/$(TARGET_NAME)" -rpath @executable_path/../Frameworks/ -target arm64-apple-macos13.3 -mmacosx-version-min=13.3 -D_LIBCPP_DISABLE_AVAILABILITY

# Firma la aplicación
	codesign --deep --force --sign - --timestamp=none "$(RELEASE_FOLDER)/$(APP_NAME).app"

# Empaqueta el .dmg de la versión Apple Silicon con create-dmg
	@echo "Creando DMG Apple Silicon con iconos de 96x96..."
	create-dmg \
	  --volname "$(APP_NAME)" \
	  --window-pos 200 120 \
	  --window-size 720 300 \
	  --icon-size 96 \
	  --text-size 12 \
	  --icon "$(APP_NAME).app" 278 102 \
	  --icon "LICENSE" 441 102 \
	  --icon "README.md" 604 102 \
	  --app-drop-link 115 102 \
	  --hide-extension "$(APP_NAME).app" \
	  "$(MACOS_APPLE_SILICON_RELEASE)" \
	  "$(RELEASE_FOLDER)" || true
	@echo "Release Apple Silicon creado: $(MACOS_APPLE_SILICON_RELEASE)"

# Elimina las carpetas temporales
	$(RMDIR) Frameworks
	$(RMDIR) "$(RELEASE_FOLDER)"

# ==============================================================================
# COMPILACIÓN PARA LINUX
# ==============================================================================
linux:
	@echo "Compilando para Linux: $(TARGET_NAME)"
	g++ $(ALL_SOURCES) $(INCLUDES) $(CXXFLAGS) $(LDFLAGS) -o "$(TARGET_FILE)"
	strip -s -R .comment -R .gnu.version "$(TARGET_FILE)" --strip-unneeded

linux_debug:
	@echo "Compilando version debug para Linux: $(TARGET_NAME)_debug"
	g++ $(ALL_SOURCES) $(INCLUDES) -DDEBUG -DVERBOSE $(CXXFLAGS_DEBUG) $(LDFLAGS) -o "$(TARGET_FILE)_debug"

linux_release:
	@$(MAKE) pack_tool
	@$(MAKE) resources.pack
	@echo "Creando release para Linux - Version: $(VERSION)"

# Generate project.h from project.h.in
	@echo "Generando project.h..."
	@GIT_HASH=$$(git rev-parse --short=7 HEAD 2>/dev/null || echo "unknown"); \
	RAW_VERSION=$$(echo "$(VERSION)" | sed 's/^v//'); \
	COPYRIGHT=$$(sed -n 's/.*set(PROJECT_COPYRIGHT "\([^"]*\)".*/\1/p' CMakeLists.txt); \
	sed -e "s/@PROJECT_NAME@/$(TARGET_NAME)/g" \
	    -e "s/@PROJECT_LONG_NAME@/$(LONG_NAME)/g" \
	    -e "s/@PROJECT_VERSION@/$$RAW_VERSION/g" \
	    -e "s/@PROJECT_COPYRIGHT@/$$COPYRIGHT/g" \
	    -e "s/@GIT_HASH@/$$GIT_HASH/g" \
	    source/project.h.in > source/project.h

# Elimina carpetas previas
	$(RMDIR) "$(RELEASE_FOLDER)"

# Crea la carpeta temporal para realizar el lanzamiento
	$(MKDIR) "$(RELEASE_FOLDER)"

# Copia ficheros
	cp resources.pack "$(RELEASE_FOLDER)"
	cp LICENSE "$(RELEASE_FOLDER)"
	cp README.md "$(RELEASE_FOLDER)"
	cp gamecontrollerdb.txt "$(RELEASE_FOLDER)"

# Compila
	g++ $(ALL_SOURCES) $(INCLUDES) -DRELEASE_BUILD $(CXXFLAGS) $(LDFLAGS) -o "$(RELEASE_FILE)"
	strip -s -R .comment -R .gnu.version "$(RELEASE_FILE)" --strip-unneeded

# Empaqueta ficheros
	$(RMFILE) "$(LINUX_RELEASE)"
	tar -czvf "$(LINUX_RELEASE)" -C "$(RELEASE_FOLDER)" .
	@echo "Release creado: $(LINUX_RELEASE)"

# Elimina la carpeta temporal
	$(RMDIR) "$(RELEASE_FOLDER)"

# ==============================================================================
# COMPILACIÓN PARA RASPBERRY PI (ARM64)
# ==============================================================================
RPI_CXX := aarch64-linux-gnu-g++
RPI_STRIP := aarch64-linux-gnu-strip
RPI_CXXFLAGS := -std=$(CPP_STANDARD) -Wall -Os -ffunction-sections -fdata-sections -DLINUX_BUILD -DRPI_BUILD
RPI_LDFLAGS := -lSDL3 -lGL

rpi_release:
	@$(MAKE) pack_tool
	@$(MAKE) resources.pack
	@echo "Creando release para Raspberry Pi (ARM64) - Version: $(VERSION)"

# Generate project.h from project.h.in
	@echo "Generando project.h..."
	@GIT_HASH=$$(git rev-parse --short=7 HEAD 2>/dev/null || echo "unknown"); \
	RAW_VERSION=$$(echo "$(VERSION)" | sed 's/^v//'); \
	COPYRIGHT=$$(sed -n 's/.*set(PROJECT_COPYRIGHT "\([^"]*\)".*/\1/p' CMakeLists.txt); \
	sed -e "s/@PROJECT_NAME@/$(TARGET_NAME)/g" \
	    -e "s/@PROJECT_LONG_NAME@/$(LONG_NAME)/g" \
	    -e "s/@PROJECT_VERSION@/$$RAW_VERSION/g" \
	    -e "s/@PROJECT_COPYRIGHT@/$$COPYRIGHT/g" \
	    -e "s/@GIT_HASH@/$$GIT_HASH/g" \
	    source/project.h.in > source/project.h

# Elimina carpetas previas
	$(RMDIR) "$(RELEASE_FOLDER)"

# Crea la carpeta temporal para realizar el lanzamiento
	$(MKDIR) "$(RELEASE_FOLDER)"

# Copia ficheros
	cp resources.pack "$(RELEASE_FOLDER)"
	cp LICENSE "$(RELEASE_FOLDER)"
	cp README.md "$(RELEASE_FOLDER)"
	cp gamecontrollerdb.txt "$(RELEASE_FOLDER)"

# Compila con cross-compiler ARM64
	$(RPI_CXX) $(ALL_SOURCES) $(INCLUDES) -DRELEASE_BUILD $(RPI_CXXFLAGS) $(RPI_LDFLAGS) -o "$(RELEASE_FILE)"
	$(RPI_STRIP) -s -R .comment -R .gnu.version "$(RELEASE_FILE)" --strip-unneeded

# Empaqueta ficheros
	$(RMFILE) "$(RPI_RELEASE)"
	tar -czvf "$(RPI_RELEASE)" -C "$(RELEASE_FOLDER)" .
	@echo "Release creado: $(RPI_RELEASE)"

# Elimina la carpeta temporal
	$(RMDIR) "$(RELEASE_FOLDER)"

# ==============================================================================
# CROSS-COMPILATION (Linux -> Windows)
# ==============================================================================
CROSS_CXX       := x86_64-w64-mingw32-g++
CROSS_STRIP     := x86_64-w64-mingw32-strip
CROSS_WINDRES   := x86_64-w64-mingw32-windres
CROSS_ZIP_FILE  := $(TARGET_NAME)-$(VERSION)-win32-x64.zip

CROSS_CXXFLAGS  := -std=$(CPP_STANDARD) -Wall -Os -ffunction-sections -fdata-sections \
                   -Wl,--gc-sections -static-libstdc++ -static-libgcc \
                   -Wl,-subsystem,windows -DWINDOWS_BUILD -DRELEASE_BUILD
CROSS_LDFLAGS   := -lmingw32 -lws2_32 -lSDL3 -lopengl32

windows_cross:
	@$(MAKE) pack_tool
	@$(MAKE) resources.pack
	@echo "Cross-compiling para Windows desde Linux..."

# 1. Generar project.h
	@GIT_HASH=$$(git rev-parse --short=7 HEAD 2>/dev/null || echo "unknown"); \
	RAW_VERSION=$$(echo "$(VERSION)" | sed 's/^v//'); \
	COPYRIGHT=$$(sed -n 's/.*set(PROJECT_COPYRIGHT "\([^"]*\)".*/\1/p' CMakeLists.txt); \
	sed -e "s/@PROJECT_NAME@/$(TARGET_NAME)/g" \
	    -e "s/@PROJECT_LONG_NAME@/$(LONG_NAME)/g" \
	    -e "s/@PROJECT_VERSION@/$$RAW_VERSION/g" \
	    -e "s/@PROJECT_COPYRIGHT@/$$COPYRIGHT/g" \
	    -e "s/@GIT_HASH@/$$GIT_HASH/g" \
	    source/project.h.in > source/project.h

# 2. Preparar carpetas
	$(RMDIR) "$(RELEASE_FOLDER)"
	$(MKDIR) "$(RELEASE_FOLDER)"

# 3. Copiar recursos base
	cp resources.pack "$(RELEASE_FOLDER)"
	cp LICENSE "$(RELEASE_FOLDER)"
	cp README.md "$(RELEASE_FOLDER)"
	cp gamecontrollerdb.txt "$(RELEASE_FOLDER)"

# 4. GESTIÓN DE DLLs (Aquí está la mejora)
# [cite_start]Copia las DLLs locales si existen (como en tu windows_release) [cite: 16]
	@if [ -d "release/dll" ] && [ "$$(ls -A release/dll/*.dll 2>/dev/null)" ]; then \
		cp release/dll/*.dll "$(RELEASE_FOLDER)"; \
		echo "Copiadas DLLs locales desde release/dll/"; \
	fi
# Copia la DLL de SDL3 externa si se pasa como argumento (Para el CI)
	@if [ -n "$(SDL_DLL_PATH)" ]; then \
		cp "$(SDL_DLL_PATH)" "$(RELEASE_FOLDER)"; \
		echo "Copiada SDL3 DLL desde $(SDL_DLL_PATH)"; \
	fi

# 5. Compilar
	@echo "Compilando recursos e icono..."
	$(CROSS_WINDRES) release/$(TARGET_NAME).rc -O coff -o $(RESOURCE_FILE)
	@echo "Compilando ejecutable..."
	$(CROSS_CXX) $(ALL_SOURCES) $(RESOURCE_FILE) $(INCLUDES) $(CROSS_CXXFLAGS) $(CROSS_LDFLAGS) -o "$(RELEASE_FOLDER)/$(APP_NAME).exe"
	$(CROSS_STRIP) -s "$(RELEASE_FOLDER)/$(APP_NAME).exe"

# 6. Empaquetar
	@echo "Empaquetando en $(CROSS_ZIP_FILE)..."
	cd $(RELEASE_FOLDER) && zip -r ../$(CROSS_ZIP_FILE) .
	$(RMDIR) "$(RELEASE_FOLDER)"
	@echo "✓ Release de Windows (Cross) completado: $(CROSS_ZIP_FILE)"

# ==============================================================================
# SETUP
# ==============================================================================
setup_hooks:
	@echo "Configurando git hooks..."
	git config core.hooksPath tools/hooks
	@echo "✓ Git hooks configurados desde tools/hooks/"

# ==============================================================================
# REGLAS ESPECIALES
# ==============================================================================
# Regla para mostrar la versión actual
show_version:
	@echo "Version actual: $(VERSION)"

# Regla de ayuda
help:
	@echo "Makefile para $(LONG_NAME)"
	@echo "Comandos disponibles:"
	@echo "  windows          - Compilar para Windows"
	@echo "  windows_debug    - Compilar debug para Windows"
	@echo "  windows_release  - Crear release completo para Windows"
	@echo "  linux            - Compilar para Linux"
	@echo "  linux_debug      - Compilar debug para Linux"
	@echo "  linux_release    - Crear release completo para Linux"
	@echo "  rpi_release      - Crear release para Raspberry Pi (ARM64)"
	@echo "  macos            - Compilar para macOS"
	@echo "  macos_debug      - Compilar debug para macOS"
	@echo "  macos_release    - Crear release completo para macOS"
	@echo "  pack_tool        - Compilar herramienta de empaquetado"
	@echo "  resources.pack   - Generar pack de recursos desde data/"
	@echo "  setup_hooks      - Configurar git hooks (pre-commit)"
	@echo "  show_version     - Mostrar version actual ($(VERSION))"
	@echo "  help             - Mostrar esta ayuda"

FORCE:

.PHONY: windows windows_debug windows_release macos macos_debug macos_release linux linux_debug linux_release rpi_release pack_tool resources.pack setup_hooks show_version help
