cmake_minimum_required(VERSION 3.25)
project(ModernEngine LANGUAGES CXX C)  # Add C for glad.c

# Use the latest C++ standard (C++23)
set(CMAKE_CXX_STANDARD 23)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

# Add compile flags for modern C++
if(MSVC)
    add_compile_options(/W4)
else()
    add_compile_options(-Wall -Wextra -Wpedantic)
endif()

# Find required packages
find_package(PkgConfig REQUIRED)
find_package(OpenGL REQUIRED)

# Find GLFW via pkg-config
pkg_check_modules(GLFW REQUIRED glfw3)
link_directories(${GLFW_LIBRARY_DIRS})

# Find GLM (Homebrew or system)
find_package(glm REQUIRED)
if(NOT DEFINED GLM_INCLUDE_DIRS)
    find_path(GLM_INCLUDE_DIRS
        glm/glm.hpp
        PATHS /opt/homebrew/include /usr/local/include ${CMAKE_PREFIX_PATH}
        NO_DEFAULT_PATH
    )
endif()
if(NOT GLM_INCLUDE_DIRS)
    message(FATAL_ERROR "Could not find glm headers (glm/glm.hpp). Install with 'brew install glm'.")
endif()
message(STATUS "Using GLM include dir: ${GLM_INCLUDE_DIRS}")

# GLAD setup - check multiple possible locations
set(GLAD_INCLUDE_DIR ${CMAKE_SOURCE_DIR}/include)
set(GLAD_SOURCE_CANDIDATES
    ${CMAKE_SOURCE_DIR}/src/glad.c
    ${CMAKE_SOURCE_DIR}/external/glad/src/glad.c
    ${CMAKE_SOURCE_DIR}/glad/src/glad.c
)

# Find GLAD source file
set(GLAD_SOURCE "")
foreach(candidate ${GLAD_SOURCE_CANDIDATES})
    if(EXISTS ${candidate})
        set(GLAD_SOURCE ${candidate})
        message(STATUS "Found GLAD source: ${GLAD_SOURCE}")
        break()
    endif()
endforeach()

if(NOT GLAD_SOURCE)
    message(FATAL_ERROR "Could not find glad.c in any of the expected locations: ${GLAD_SOURCE_CANDIDATES}")
endif()

# Collect engine source files (exclude glad.c from glob to avoid duplication)
file(GLOB_RECURSE ENGINE_SOURCES
    ${CMAKE_SOURCE_DIR}/src/*.cpp
)

# Remove glad.c from ENGINE_SOURCES if it was included by glob
list(FILTER ENGINE_SOURCES EXCLUDE REGEX ".*glad\\.c$")

# Create executable target
add_executable(${PROJECT_NAME}
    ${ENGINE_SOURCES}
    ${GLAD_SOURCE}
)

# Include directories
target_include_directories(${PROJECT_NAME}
    PRIVATE
    ${CMAKE_SOURCE_DIR}/include  # For glad.h
    ${GLFW_INCLUDE_DIRS}
    ${GLM_INCLUDE_DIRS}
)

# Link libraries and macOS frameworks
target_link_libraries(${PROJECT_NAME}
    PRIVATE
    OpenGL::GL
    ${GLFW_LIBRARIES}
)

# macOS-specific frameworks
if(APPLE)
    target_link_libraries(${PROJECT_NAME}
        PRIVATE
        "-framework Cocoa"
        "-framework IOKit"
        "-framework CoreVideo"
        "-framework QuartzCore"
        "-framework AudioToolbox"
        "-framework CoreAudio"
    )
endif()

# Copy assets to build directory if assets exist
if(EXISTS ${CMAKE_SOURCE_DIR}/assets)
    file(COPY ${CMAKE_SOURCE_DIR}/assets DESTINATION ${CMAKE_BINARY_DIR})
endif()

# Windows-specific properties
if(WIN32)
    set_target_properties(${PROJECT_NAME} PROPERTIES
        WIN32_EXECUTABLE TRUE
    )
endif()

# Debug information
message(STATUS "Engine sources found: ${ENGINE_SOURCES}")
message(STATUS "GLAD source: ${GLAD_SOURCE}")
