# ---------------------------------------------------------------------------
# Paths
# ---------------------------------------------------------------------------
set(OBERON_SRC_DIR  "${CMAKE_CURRENT_SOURCE_DIR}/src")
set(OBERON_EXAMPLES "${CMAKE_CURRENT_SOURCE_DIR}/examples")
set(OBERON_TESTS    "${CMAKE_CURRENT_SOURCE_DIR}/test")
set(OBERON_TESTS_PY "${CMAKE_CURRENT_SOURCE_DIR}/tests")

# Python3 is inherited from the root CMakeLists.txt
if(NOT Python3_EXECUTABLE)
    find_package(Python3 3.8 REQUIRED COMPONENTS Interpreter)
endif()

set(COMPILER_MAIN "${OBERON_SRC_DIR}/oberonc.py")

# ---------------------------------------------------------------------------
# Compiler validation target
# ---------------------------------------------------------------------------

add_custom_target(compiler-check
    COMMAND ${Python3_EXECUTABLE} -c
        "import sys; sys.path.insert(0,'${OBERON_SRC_DIR}'); \
         from lexer import Lexer; from parser import Parser; \
         from codegen import CodeGenerator; from symboltable import SymbolTable; \
         print('Compiler modules OK')"
    COMMENT "Validating Oberon compiler modules"
    VERBATIM
)

# ---------------------------------------------------------------------------
# Helper macro: compile a single .obr file to .asm
# ---------------------------------------------------------------------------

macro(add_oberon_program TARGET_NAME OBR_FILE)
    get_filename_component(_basename "${OBR_FILE}" NAME_WE)
    set(_asm_out "${CMAKE_CURRENT_BINARY_DIR}/${_basename}.asm")

    add_custom_command(
        OUTPUT  "${_asm_out}"
        COMMAND ${Python3_EXECUTABLE} "${COMPILER_MAIN}"
                    "${OBR_FILE}"
                    -o "${_asm_out}"
        DEPENDS "${OBR_FILE}" "${COMPILER_MAIN}"
        COMMENT "Compiling ${_basename}.obr -> ${_basename}.asm"
        VERBATIM
    )

    add_custom_target("obr_${TARGET_NAME}" DEPENDS "${_asm_out}")
endmacro()

# ---------------------------------------------------------------------------
# Example programs
# ---------------------------------------------------------------------------

if(BUILD_EXAMPLES)
    file(GLOB _EXAMPLE_OBR "${OBERON_EXAMPLES}/*.obr")
    foreach(_obr ${_EXAMPLE_OBR})
        get_filename_component(_name "${_obr}" NAME_WE)
        add_oberon_program("example_${_name}" "${_obr}")
    endforeach()

    file(GLOB _TEST_OBR "${OBERON_TESTS}/*.obr")
    foreach(_obr ${_TEST_OBR})
        get_filename_component(_name "${_obr}" NAME_WE)
        add_oberon_program("test_${_name}" "${_obr}")
    endforeach()

    # Convenience target: build all examples
    file(GLOB _ALL_OBR
        "${OBERON_EXAMPLES}/*.obr"
        "${OBERON_TESTS}/*.obr"
    )
    set(_all_asm_targets)
    foreach(_obr ${_ALL_OBR})
        get_filename_component(_n "${_obr}" NAME_WE)
        list(APPEND _all_asm_targets "obr_example_${_n}" "obr_test_${_n}")
    endforeach()

    add_custom_target(all-oberon-examples ALL
        COMMENT "Building all Oberon example programs"
    )
endif()

# ---------------------------------------------------------------------------
# CTest: compile each example and check it produces valid assembly
#
# Programs that use unsupported Oberon features (IMPORT, TYPE, CONST, RECORD,
# ARRAY, BOOLEAN, FOR, …) are excluded from the compiler smoke tests.
# ---------------------------------------------------------------------------

# Features beyond the current compiler subset
set(_UNSUPPORTED_EXAMPLES
    warehouse   # uses IMPORT Out, TYPE, CONST, RECORD, ARRAY, BOOLEAN, FOR
)

if(BUILD_TESTING)
    # hello.obr — minimal smoke test
    set(_HELLO "${OBERON_EXAMPLES}/hello.obr")
    if(EXISTS "${_HELLO}")
        add_test(
            NAME oberon_compile_hello
            COMMAND ${Python3_EXECUTABLE} "${COMPILER_MAIN}" "${_HELLO}"
                    -o "${CMAKE_CURRENT_BINARY_DIR}/hello_test.asm"
            WORKING_DIRECTORY "${OBERON_SRC_DIR}"
        )
        set_tests_properties(oberon_compile_hello PROPERTIES
            LABELS "compiler;smoke"
            TIMEOUT 15
        )
    endif()

    # Compile each example file (skip unsupported ones)
    file(GLOB _EXAMPLES "${OBERON_EXAMPLES}/*.obr")
    foreach(_obr ${_EXAMPLES})
        get_filename_component(_n "${_obr}" NAME_WE)
        if(_n IN_LIST _UNSUPPORTED_EXAMPLES)
            continue()
        endif()
        add_test(
            NAME "oberon_compile_example_${_n}"
            COMMAND ${Python3_EXECUTABLE} "${COMPILER_MAIN}" "${_obr}"
                    -o "${CMAKE_CURRENT_BINARY_DIR}/${_n}_ci.asm"
            WORKING_DIRECTORY "${OBERON_SRC_DIR}"
        )
        set_tests_properties("oberon_compile_example_${_n}" PROPERTIES
            LABELS "compiler;examples"
            TIMEOUT 15
        )
    endforeach()

    # Compile each test program
    file(GLOB _TEST_PROGS "${OBERON_TESTS}/*.obr")
    foreach(_obr ${_TEST_PROGS})
        get_filename_component(_n "${_obr}" NAME_WE)
        add_test(
            NAME "oberon_compile_testprog_${_n}"
            COMMAND ${Python3_EXECUTABLE} "${COMPILER_MAIN}" "${_obr}"
                    -o "${CMAKE_CURRENT_BINARY_DIR}/${_n}_ci.asm"
            WORKING_DIRECTORY "${OBERON_SRC_DIR}"
        )
        set_tests_properties("oberon_compile_testprog_${_n}" PROPERTIES
            LABELS "compiler;test-programs"
            TIMEOUT 15
        )
    endforeach()

    # Python test suite via pytest
    add_subdirectory(tests)
endif()
