# Production Build Configuration
# This file configures CMake for optimized release builds

# Set release flags and optimizations
if(CMAKE_BUILD_TYPE STREQUAL "Release")
    message(STATUS "Configuring Release build with optimizations")
    
    # Enable aggressive optimizations for release
    if(MSVC)
        # MSVC specific optimizations
        set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /O2 /Ob2 /DNDEBUG")
        set(CMAKE_EXE_LINKER_FLAGS_RELEASE "${CMAKE_EXE_LINKER_FLAGS_RELEASE} /OPT:REF /OPT:ICF")
        
        # Enable whole program optimization
        # Detect game target (spacetris only)
        if(TARGET spacetris)
            set(GAME_TARGET spacetris)
        else()
            message(FATAL_ERROR "No game target found (expected 'spacetris')")
        endif()

        set_target_properties(${GAME_TARGET} PROPERTIES 
            INTERPROCEDURAL_OPTIMIZATION_RELEASE TRUE
        )

        # Set subsystem to Windows (no console) for release
        set_target_properties(${GAME_TARGET} PROPERTIES
            WIN32_EXECUTABLE TRUE
            LINK_FLAGS_RELEASE "/SUBSYSTEM:WINDOWS /ENTRY:mainCRTStartup"
        )
    endif()
    
    if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU" OR CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
        # GCC/Clang optimizations
        set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -O3 -DNDEBUG -flto")
        set(CMAKE_EXE_LINKER_FLAGS_RELEASE "${CMAKE_EXE_LINKER_FLAGS_RELEASE} -flto")
    endif()
endif()

# Ensure we have a GAME_TARGET set (spacetris only)
if(NOT DEFINED GAME_TARGET)
    if(TARGET spacetris)
        set(GAME_TARGET spacetris)
    else()
        message(FATAL_ERROR "No game target found (expected 'spacetris')")
    endif()
endif()

# Custom target for creating distribution package (renamed to avoid conflict with CPack)
if(WIN32)
    # Windows-specific packaging
    if(GAME_TARGET)
        add_custom_target(dist_package
            COMMAND ${CMAKE_COMMAND} -E echo "Creating Windows distribution package..."
            COMMAND ${CMAKE_COMMAND} -E make_directory "${CMAKE_BINARY_DIR}/package/SpacetrisGame"
            COMMAND ${CMAKE_COMMAND} -E copy "$<TARGET_FILE:${GAME_TARGET}>" "${CMAKE_BINARY_DIR}/package/SpacetrisGame/"
            COMMAND ${CMAKE_COMMAND} -E copy_directory "${CMAKE_SOURCE_DIR}/assets" "${CMAKE_BINARY_DIR}/package/SpacetrisGame/assets"
            COMMAND ${CMAKE_COMMAND} -E copy_if_different "${CMAKE_SOURCE_DIR}/FreeSans.ttf" "${CMAKE_BINARY_DIR}/package/SpacetrisGame/"
            COMMENT "Packaging Spacetris for distribution"
            DEPENDS ${GAME_TARGET}
        )
    else()
        message(WARNING "No game target detected; skipping dist_package target.")
    endif()
    
    # Try to copy SDL DLLs automatically (SDL_image no longer needed)
    find_file(SDL3_DLL SDL3.dll PATHS "${CMAKE_BINARY_DIR}/vcpkg_installed/x64-windows/bin" NO_DEFAULT_PATH)
    find_file(SDL3_TTF_DLL SDL3_ttf.dll PATHS "${CMAKE_BINARY_DIR}/vcpkg_installed/x64-windows/bin" NO_DEFAULT_PATH)

    if(GAME_TARGET)
        if(SDL3_DLL)
            add_custom_command(TARGET dist_package POST_BUILD
                COMMAND ${CMAKE_COMMAND} -E copy_if_different "${SDL3_DLL}" "${CMAKE_BINARY_DIR}/package/SpacetrisGame/"
                COMMENT "Copying SDL3.dll"
            )
        endif()

        if(SDL3_TTF_DLL)
            add_custom_command(TARGET dist_package POST_BUILD
                COMMAND ${CMAKE_COMMAND} -E copy_if_different "${SDL3_TTF_DLL}" "${CMAKE_BINARY_DIR}/package/SpacetrisGame/"
                COMMENT "Copying SDL3_ttf.dll"
            )
        endif()
    endif()
endif()

# Installation rules for system-wide installation
if(GAME_TARGET)
    if(APPLE)
        install(TARGETS ${GAME_TARGET}
            BUNDLE DESTINATION .
            COMPONENT Runtime
        )
    else()
        install(TARGETS ${GAME_TARGET} 
            RUNTIME DESTINATION bin
            COMPONENT Runtime
        )
    endif()

    install(DIRECTORY assets/
        DESTINATION share/${GAME_TARGET}/assets
        COMPONENT Runtime
    )

    install(FILES FreeSans.ttf
        DESTINATION share/${GAME_TARGET}
        COMPONENT Runtime
    )
endif()

# CPack configuration for creating installers (commented out - requires LICENSE file)
# set(CPACK_PACKAGE_NAME "Tetris")
# set(CPACK_PACKAGE_VENDOR "TetrisGame")
# set(CPACK_PACKAGE_VERSION_MAJOR "1")
# set(CPACK_PACKAGE_VERSION_MINOR "0")
# set(CPACK_PACKAGE_VERSION_PATCH "0")
# set(CPACK_PACKAGE_DESCRIPTION_SUMMARY "Classic Tetris game built with SDL3")
# set(CPACK_RESOURCE_FILE_LICENSE "${CMAKE_SOURCE_DIR}/LICENSE")

# if(WIN32)
#     set(CPACK_GENERATOR "ZIP;NSIS")
#     set(CPACK_NSIS_DISPLAY_NAME "Tetris Game")
#     set(CPACK_NSIS_PACKAGE_NAME "Tetris")
#     set(CPACK_NSIS_CONTACT "tetris@example.com")
#     set(CPACK_NSIS_MODIFY_PATH OFF)
# endif()

# include(CPack)
