# CMakeLists.txt

cmake_minimum_required(VERSION 3.10)
project(aee VERSION 1.00)

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

# --- LISTA EXPLÍCITA DE FUENTES ---
set(APP_SOURCES
    # Core - Motor original "Jail" (no tocar gameplay)
    source/core/jail/jail_audio.cpp
    source/core/jail/jdraw8.cpp
    source/core/jail/jfile.cpp
    source/core/jail/jgame.cpp
    source/core/jail/jinput.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/global_inputs.cpp
    source/core/input/mouse.cpp

    # Core - System (nova capa)
    source/core/system/director.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/modulesequence.cpp
    source/game/momia.cpp
    source/game/prota.cpp
    source/game/sprite.cpp

    # Utils
    source/utils/utils.cpp

    # Main
    source/main.cpp
)

# Configuración de SDL3
find_package(SDL3 REQUIRED CONFIG REQUIRED COMPONENTS SDL3)
message(STATUS "SDL3 encontrado: ${SDL3_INCLUDE_DIRS}")

# --- COMPILACIÓ SHADERS SPIR-V (Linux/Windows — macOS usa Metal) ---
if(NOT APPLE)
    find_program(GLSLC_EXE NAMES glslc)

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

    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()
else()
    message(STATUS "macOS: shaders SPIR-V omesos (usa Metal)")
endif()

# --- EJECUTABLE ---
add_executable(${PROJECT_NAME} ${APP_SOURCES})

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

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

# Enlazar SDL3
target_link_libraries(${PROJECT_NAME} PRIVATE SDL3::SDL3)

# --- 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)
endif()

# Ejecutable en la raíz del proyecto
set_target_properties(${PROJECT_NAME} PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR})

# --- CLANG-FORMAT TARGETS ---
find_program(CLANG_FORMAT_EXE NAMES clang-format)

# Recopilar todos los archivos fuente para formateo (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/.*")

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()
