# CMakeLists.txt

cmake_minimum_required(VERSION 3.10)
project(aee VERSION 1.00)

# Tipus de build per defecte (Debug) si no se n'ha especificat cap
if(NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES)
    set(CMAKE_BUILD_TYPE Debug CACHE STRING "" FORCE)
endif()

# Estándar de C++
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED True)

# Exportar comandos de compilación para herramientas de análisis
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

# --- GENERACIÓ AUTOMÀTICA DE VERSIÓ ---
# Si GIT_HASH ve de fora (p. ex. el Makefile via -DGIT_HASH=xxx), l'usem tal
# qual. Això evita problemes amb Docker/emscripten on git avorta per
# "dubious ownership" al volum muntat. En builds locals sense -DGIT_HASH
# resolem ací executant git directament.
if(NOT DEFINED GIT_HASH OR GIT_HASH STREQUAL "")
    find_package(Git QUIET)
    if(GIT_FOUND)
        execute_process(
            COMMAND ${GIT_EXECUTABLE} rev-parse --short=7 HEAD
            WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
            OUTPUT_VARIABLE GIT_HASH
            OUTPUT_STRIP_TRAILING_WHITESPACE
            ERROR_QUIET
        )
    endif()
    if(NOT DEFINED GIT_HASH OR GIT_HASH STREQUAL "")
        set(GIT_HASH "unknown")
    endif()
endif()

configure_file(${CMAKE_SOURCE_DIR}/source/version.h.in ${CMAKE_BINARY_DIR}/version.h @ONLY)

# --- LISTA EXPLÍCITA DE FUENTES ---
set(APP_SOURCES
    # Core - Motor original "Jail" (no tocar gameplay)
    source/core/jail/jdraw8.cpp
    source/core/jail/jfile.cpp
    source/core/jail/jgame.cpp
    source/core/jail/jinput.cpp

    # Core - Audio (wrapper canònic compartit amb la resta de projectes)
    source/core/audio/audio.cpp
    source/core/audio/audio_adapter.cpp

    # Core - Locale (nova capa)
    source/core/locale/locale.cpp

    # Core - Resources (pack binari AEE1 + cache d'assets precarregats)
    source/core/resources/resource_pack.cpp
    source/core/resources/resource_helper.cpp
    source/core/resources/resource_list.cpp
    source/core/resources/resource_cache.cpp

    # Core - Capa de presentación (nueva)
    source/core/rendering/menu.cpp
    source/core/rendering/overlay.cpp
    source/core/rendering/screen.cpp
    source/core/rendering/text.cpp

    # Core - SDL3 GPU shader backend
    source/core/rendering/sdl3gpu/sdl3gpu_shader.cpp

    # Core - Input (nova capa)
    source/core/input/gamepad.cpp
    source/core/input/global_inputs.cpp
    source/core/input/key_config.cpp
    source/core/input/key_remap.cpp
    source/core/input/mouse.cpp

    # Core - System (nova capa)
    source/core/system/director.cpp

    # Scenes (cinemàtiques i menús reescrits)
    source/game/scenes/timeline.cpp
    source/game/scenes/sprite_mover.cpp
    source/game/scenes/frame_animator.cpp
    source/game/scenes/palette_fade.cpp
    source/game/scenes/surface_handle.cpp
    source/game/scenes/scene_registry.cpp
    source/game/scenes/scene_utils.cpp
    source/game/scenes/boot_loader_scene.cpp
    source/game/scenes/mort_scene.cpp
    source/game/scenes/banner_scene.cpp
    source/game/scenes/menu_scene.cpp
    source/game/scenes/intro_new_logo_scene.cpp
    source/game/scenes/intro_scene.cpp
    source/game/scenes/intro_sprites_scene.cpp
    source/game/scenes/slides_scene.cpp
    source/game/scenes/credits_scene.cpp
    source/game/scenes/secreta_scene.cpp

    # Game
    source/game/options.cpp
    source/game/bola.cpp
    source/game/engendro.cpp
    source/game/info.cpp
    source/game/mapa.cpp
    source/game/marcador.cpp
    source/game/modulegame.cpp
    source/game/momia.cpp
    source/game/prota.cpp
    source/game/sprite.cpp

    # Utils
    source/utils/easing.cpp
    source/utils/utils.cpp

    # Main
    source/main.cpp
)

# Configuración de SDL3
# En macOS bundle mode usamos el xcframework (universal arm64+x86_64).
# En emscripten compilamos SDL3 desde source con FetchContent (no hi ha paquet de sistema).
# En el resto de casos, usamos SDL3 del sistema via find_package.
if(EMSCRIPTEN)
    include(FetchContent)
    FetchContent_Declare(
        SDL3
        GIT_REPOSITORY https://github.com/libsdl-org/SDL.git
        GIT_TAG release-3.4.4
        GIT_SHALLOW TRUE
    )
    set(SDL_SHARED OFF CACHE BOOL "" FORCE)
    set(SDL_STATIC ON CACHE BOOL "" FORCE)
    set(SDL_TEST_LIBRARY OFF CACHE BOOL "" FORCE)
    FetchContent_MakeAvailable(SDL3)
    message(STATUS "SDL3: compilat des de source per a Emscripten (FetchContent)")
elseif(APPLE AND MACOS_BUNDLE)
    set(SDL3_XCFRAMEWORK_SLICE "${CMAKE_SOURCE_DIR}/release/macos/frameworks/SDL3.xcframework/macos-arm64_x86_64")
    message(STATUS "SDL3: usando xcframework (${SDL3_XCFRAMEWORK_SLICE})")
else()
    find_package(SDL3 REQUIRED CONFIG REQUIRED COMPONENTS SDL3)
    message(STATUS "SDL3 encontrado: ${SDL3_INCLUDE_DIRS}")
endif()

# --- COMPILACIÓ SHADERS SPIR-V (Linux/Windows — macOS usa Metal, Emscripten no suporta SDL3 GPU) ---
if(NOT APPLE AND NOT EMSCRIPTEN)
    find_program(GLSLC_EXE NAMES glslc)

    set(SHADERS_DIR "${CMAKE_SOURCE_DIR}/data/shaders")
    set(HEADERS_DIR "${CMAKE_SOURCE_DIR}/source/core/rendering/sdl3gpu/spv")

    set(ALL_SHADER_HEADERS
        "${HEADERS_DIR}/postfx_vert_spv.h"
        "${HEADERS_DIR}/postfx_frag_spv.h"
        "${HEADERS_DIR}/upscale_frag_spv.h"
        "${HEADERS_DIR}/downscale_frag_spv.h"
        "${HEADERS_DIR}/crtpi_frag_spv.h"
    )
    set(ALL_SHADER_SOURCES
        "${SHADERS_DIR}/postfx.vert"
        "${SHADERS_DIR}/postfx.frag"
        "${SHADERS_DIR}/upscale.frag"
        "${SHADERS_DIR}/downscale.frag"
        "${SHADERS_DIR}/crtpi_frag.glsl"
    )

    if(GLSLC_EXE)
        add_custom_command(
            OUTPUT  ${ALL_SHADER_HEADERS}
            COMMAND ${CMAKE_COMMAND}
                    -D GLSLC=${GLSLC_EXE}
                    -D SHADERS_DIR=${SHADERS_DIR}
                    -D HEADERS_DIR=${HEADERS_DIR}
                    -P ${CMAKE_SOURCE_DIR}/tools/shaders/compile_spirv.cmake
            DEPENDS ${ALL_SHADER_SOURCES}
            WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}"
            COMMENT "Compilant shaders SPIR-V..."
        )
        add_custom_target(shaders DEPENDS ${ALL_SHADER_HEADERS})
        message(STATUS "glslc trobat: shaders es compilaran automàticament")
    else()
        foreach(HDR ${ALL_SHADER_HEADERS})
            if(NOT EXISTS "${HDR}")
                message(FATAL_ERROR
                    "glslc no trobat i header SPIR-V no existeix: ${HDR}\n"
                    "  Instal·la glslc: sudo apt install glslang-tools  (Linux)\n"
                    "                   choco install vulkan-sdk         (Windows)"
                )
            endif()
        endforeach()
        message(STATUS "glslc no trobat — usant headers SPIR-V precompilats")
    endif()
elseif(EMSCRIPTEN)
    message(STATUS "Emscripten: shaders SPIR-V omesos (SDL3 GPU no suportat a WebGL2)")
else()
    message(STATUS "macOS: shaders SPIR-V omesos (usa Metal)")
endif()

# --- EJECUTABLE ---
# A emscripten excloïm sdl3gpu_shader.cpp — SDL3 GPU no suporta WebGL2, i el
# fallback SDL_Renderer de Screen (amb NO_SHADERS) fa tota la presentació.
if(EMSCRIPTEN)
    set(APP_SOURCES_WASM ${APP_SOURCES})
    list(REMOVE_ITEM APP_SOURCES_WASM source/core/rendering/sdl3gpu/sdl3gpu_shader.cpp)
    add_executable(${PROJECT_NAME} ${APP_SOURCES_WASM})
else()
    add_executable(${PROJECT_NAME} ${APP_SOURCES})
endif()

# Shaders han de compilar-se abans que l'executable (Linux/Windows amb glslc)
if(NOT APPLE AND NOT EMSCRIPTEN AND GLSLC_EXE)
    add_dependencies(${PROJECT_NAME} shaders)
endif()

# --- DIRECTORIOS DE INCLUSIÓN ---
target_include_directories(${PROJECT_NAME} PUBLIC
    "${CMAKE_SOURCE_DIR}/source"
    "${CMAKE_BINARY_DIR}"
)

# Enlazar SDL3
if(APPLE AND MACOS_BUNDLE)
    target_compile_definitions(${PROJECT_NAME} PRIVATE MACOS_BUNDLE)
    target_include_directories(${PROJECT_NAME} PRIVATE
        "${SDL3_XCFRAMEWORK_SLICE}/SDL3.framework/Headers"
    )
    target_link_options(${PROJECT_NAME} PRIVATE
        -framework SDL3
        -F ${SDL3_XCFRAMEWORK_SLICE}
        -rpath @executable_path/../Frameworks/
    )
else()
    target_link_libraries(${PROJECT_NAME} PRIVATE SDL3::SDL3)
endif()

# --- FLAGS DE COMPILACIÓN ---
target_compile_options(${PROJECT_NAME} PRIVATE -Wall)
target_compile_options(${PROJECT_NAME} PRIVATE $<$<CONFIG:RELEASE>:-Os -ffunction-sections -fdata-sections>)

# --- CONFIGURACIÓN POR PLATAFORMA ---
if(WIN32)
    target_link_libraries(${PROJECT_NAME} PRIVATE mingw32)
elseif(EMSCRIPTEN)
    target_compile_definitions(${PROJECT_NAME} PRIVATE EMSCRIPTEN_BUILD NO_SHADERS)
    # -fexceptions: SDL3 i fkyaml llancen std::exception; sense això, `throw`
    # acaba en `abort()`. També requerit al link per congruència ABI.
    target_compile_options(${PROJECT_NAME} PRIVATE -fexceptions)
    target_link_options(${PROJECT_NAME} PRIVATE
        "SHELL:--preload-file ${CMAKE_SOURCE_DIR}/data@/data"
        -fexceptions
        -sALLOW_MEMORY_GROWTH=1
        -sMAX_WEBGL_VERSION=2
        -sINITIAL_MEMORY=67108864
        -sASSERTIONS=1
        # ASYNCIFY permet que Emscripten gestione yields durant la precarga
        # d'assets. El main loop del joc ja usa SDL3 Callback API i no depén
        # d'Asyncify — però el preloader del `.data` sí.
        -sASYNCIFY=1
    )
    set_target_properties(${PROJECT_NAME} PROPERTIES SUFFIX ".html")
endif()

# --- EINA STANDALONE: pack_resources ---
# Executable auxiliar que empaqueta `data/` a `resources.pack` (format AEE1).
# No es compila per defecte (EXCLUDE_FROM_ALL). Build explícit:
#   cmake --build build --target pack_resources
# Després executar: ./build/pack_resources data resources.pack
if(NOT EMSCRIPTEN)
    add_executable(pack_resources EXCLUDE_FROM_ALL
        tools/pack_resources/pack_resources.cpp
        source/core/resources/resource_pack.cpp
    )
    target_include_directories(pack_resources PRIVATE "${CMAKE_SOURCE_DIR}/source")
    target_compile_options(pack_resources PRIVATE -Wall)

    # --- Regeneració automàtica de resources.pack ---
    # Cada `cmake --build build` torna a empaquetar `data/` si algun fitxer ha
    # canviat. Evita debugar amb un pack obsolet. CONFIGURE_DEPENDS força CMake
    # a re-globbar a la pròxima invocació (recull fitxers nous afegits a data/).
    file(GLOB_RECURSE DATA_FILES CONFIGURE_DEPENDS "${CMAKE_SOURCE_DIR}/data/*")
    set(RESOURCE_PACK "${CMAKE_BINARY_DIR}/resources.pack")

    add_custom_command(
        OUTPUT ${RESOURCE_PACK}
        COMMAND $<TARGET_FILE:pack_resources>
            "${CMAKE_SOURCE_DIR}/data"
            "${RESOURCE_PACK}"
        DEPENDS pack_resources ${DATA_FILES}
        WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
        COMMENT "Empaquetant data/ → resources.pack"
        VERBATIM
    )

    add_custom_target(resource_pack ALL DEPENDS ${RESOURCE_PACK})
    add_dependencies(${PROJECT_NAME} resource_pack)

    # --- CÒPIA DE gamecontrollerdb.txt AL COSTAT DEL BINARI ---
    # SDL_AddGamepadMappingsFromFile només llegeix del filesystem real (no del
    # pack), així que el fitxer ha de viure al directori del binari. Es copia
    # només si existeix per no fallar la build d'algú que encara no ha fet
    # `make controllerdb`.
    if(EXISTS "${CMAKE_SOURCE_DIR}/gamecontrollerdb.txt")
        set(CONTROLLER_DB "${CMAKE_BINARY_DIR}/gamecontrollerdb.txt")
        add_custom_command(
            OUTPUT ${CONTROLLER_DB}
            COMMAND ${CMAKE_COMMAND} -E copy_if_different
                "${CMAKE_SOURCE_DIR}/gamecontrollerdb.txt"
                "${CONTROLLER_DB}"
            DEPENDS "${CMAKE_SOURCE_DIR}/gamecontrollerdb.txt"
            COMMENT "Copiant gamecontrollerdb.txt → build/"
            VERBATIM
        )
        add_custom_target(controller_db ALL DEPENDS ${CONTROLLER_DB})
        add_dependencies(${PROJECT_NAME} controller_db)
    endif()
endif()

# --- STATIC ANALYSIS TARGETS ---
find_program(CLANG_TIDY_EXE NAMES clang-tidy)
find_program(CLANG_FORMAT_EXE NAMES clang-format)
find_program(CPPCHECK_EXE NAMES cppcheck)

# Recopilar todos los archivos fuente (excluir external/)
file(GLOB_RECURSE ALL_SOURCE_FILES
    "${CMAKE_SOURCE_DIR}/source/*.cpp"
    "${CMAKE_SOURCE_DIR}/source/*.hpp"
    "${CMAKE_SOURCE_DIR}/source/*.h"
)
list(FILTER ALL_SOURCE_FILES EXCLUDE REGEX ".*/external/.*")

set(CLANG_TIDY_SOURCES ${ALL_SOURCE_FILES})

# Para cppcheck, pasar solo .cpp (los headers se procesan transitivamente).
set(CPPCHECK_SOURCES ${ALL_SOURCE_FILES})
list(FILTER CPPCHECK_SOURCES INCLUDE REGEX ".*\\.cpp$")

# Targets de clang-tidy
if(CLANG_TIDY_EXE)
    add_custom_target(tidy
        COMMAND ${CLANG_TIDY_EXE}
            -p ${CMAKE_BINARY_DIR}
            ${CLANG_TIDY_SOURCES}
        WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
        COMMENT "Running clang-tidy..."
    )

    add_custom_target(tidy-fix
        COMMAND ${CLANG_TIDY_EXE}
            -p ${CMAKE_BINARY_DIR}
            --fix
            ${CLANG_TIDY_SOURCES}
        WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
        COMMENT "Running clang-tidy with fixes..."
    )
else()
    message(STATUS "clang-tidy no encontrado - targets 'tidy' y 'tidy-fix' no disponibles")
endif()

# Targets de clang-format
if(CLANG_FORMAT_EXE)
    add_custom_target(format
        COMMAND ${CLANG_FORMAT_EXE}
            -i
            ${ALL_SOURCE_FILES}
        WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
        COMMENT "Running clang-format..."
    )

    add_custom_target(format-check
        COMMAND ${CLANG_FORMAT_EXE}
            --dry-run
            --Werror
            ${ALL_SOURCE_FILES}
        WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
        COMMENT "Checking clang-format..."
    )
else()
    message(STATUS "clang-format no encontrado - targets 'format' y 'format-check' no disponibles")
endif()

# Target de cppcheck
if(CPPCHECK_EXE)
    add_custom_target(cppcheck
        COMMAND ${CPPCHECK_EXE}
            --enable=warning,style,performance,portability
            --std=c++20
            --language=c++
            --inline-suppr
            --suppress=missingIncludeSystem
            --suppress=toomanyconfigs
            --suppress=*:*/source/external/*
            --suppress=*:*/source/core/rendering/sdl3gpu/spv/*
            --quiet
            -I ${CMAKE_SOURCE_DIR}/source
            ${CPPCHECK_SOURCES}
        WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
        COMMENT "Running cppcheck..."
    )
else()
    message(STATUS "cppcheck no encontrado - target 'cppcheck' no disponible")
endif()
