# ==============================================================================
# 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
# ==============================================================================
TARGET_NAME    := jaildoctors_dilemma
TARGET_FILE    := $(DIR_BIN)$(TARGET_NAME)
APP_NAME       := JailDoctor's Dilemma
RELEASE_FOLDER := jdd_release
RELEASE_FILE   := $(RELEASE_FOLDER)/$(TARGET_NAME)
RESOURCE_FILE  := release/jdd.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 (extracted from defines.hpp)
# ==============================================================================
ifeq ($(OS),Windows_NT)
    VERSION := v$(shell powershell -Command "(Select-String -Path 'source/utils/defines.hpp' -Pattern 'constexpr const char\* VERSION = \"(.+?)\"').Matches.Groups[1].Value")
else
    VERSION := v$(shell grep 'constexpr const char\* VERSION' source/utils/defines.hpp | sed -E 's/.*VERSION = "([^"]+)".*/\1/')
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

# ==============================================================================
# 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/cheevos.cpp \
    source/game/gameplay/item_tracker.cpp \
    source/game/gameplay/room_tracker.cpp \
    source/game/gameplay/stats.cpp \
    source/game/scenes/logo.cpp \
    source/game/scenes/loading_screen.cpp \
    source/game/scenes/title.cpp \
    source/game/scenes/game.cpp \
    source/game/scenes/game_over.cpp \
    source/game/scenes/ending.cpp \
    source/game/scenes/ending2.cpp \
    source/game/scenes/credits.cpp \
    source/game/ui/notifier.cpp \
    source/utils/utils.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/jdd.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"
	g++ $(ALL_SOURCES) $(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 version.h from version.h.in
	@echo "Generando version.h..."
	@powershell -Command "$$GIT_HASH = (git rev-parse --short=7 HEAD 2>$$null); if (-not $$GIT_HASH) { $$GIT_HASH = 'unknown' }; (Get-Content source/version.h.in) -replace '@GIT_HASH@', $$GIT_HASH | Set-Content source/version.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" -Destination "$(RELEASE_FOLDER)"

# Compila (con icono)
	windres release/jdd.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 version.h from version.h.in
	@echo "Generando version.h..."
	@GIT_HASH=$$(git rev-parse --short=7 HEAD 2>/dev/null || echo "unknown"); \
	sed "s/@GIT_HASH@/$$GIT_HASH/g" source/version.h.in > source/version.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"
	cp -R release/frameworks/SDL3.xcframework/macos-arm64_x86_64/SDL3.framework "$(RELEASE_FOLDER)/$(APP_NAME).app/Contents/Frameworks"
	cp -R release/frameworks/SDL3.xcframework/macos-arm64_x86_64/SDL3.framework Frameworks
	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-macos10.15

# 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-macos11

# 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 version.h from version.h.in
	@echo "Generando version.h..."
	@GIT_HASH=$$(git rev-parse --short=7 HEAD 2>/dev/null || echo "unknown"); \
	sed "s/@GIT_HASH@/$$GIT_HASH/g" source/version.h.in > source/version.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)"

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

# Regla de ayuda
help:
	@echo "Makefile para JailDoctor's Dilemma"
	@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 "  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 "  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 pack_tool resources.pack show_version help
