# Dependency validation and setup utilities for this project
# This module provides functions to validate and setup dependencies managed by this project

include(CheckCXXSourceCompiles)
include(CheckCXXCompilerFlag)

# Function to validate a dependency exists
function(validate_dependency dependency_name dependency_path)
    if(NOT EXISTS "${dependency_path}")
        message(FATAL_ERROR "${dependency_name} not found at ${dependency_path}. 
        Please ensure the dependency is properly installed or run the setup script.")
    endif()
    
    message(STATUS "✓ ${dependency_name} found at ${dependency_path}")
endfunction()

# Function to validate system dependencies
# Named validate_project_system_dependencies to avoid collision with neuriplo's
# validate_system_dependencies which gets loaded via FetchContent
function(validate_project_system_dependencies)
    # OpenCV and glog should already be found before this function is called
    # Just validate versions if they're already found
    if(NOT OpenCV_FOUND)
        find_package(OpenCV REQUIRED)
    endif()
    if(OpenCV_VERSION VERSION_LESS OPENCV_MIN_VERSION)
        message(FATAL_ERROR "OpenCV version ${OpenCV_VERSION} is too old. Minimum required: ${OPENCV_MIN_VERSION}")
    endif()
    message(STATUS "✓ OpenCV ${OpenCV_VERSION} validated")
    
    if(NOT glog_FOUND)
        find_package(glog REQUIRED)
    endif()
    message(STATUS "✓ glog validated")
    
    # Validate CMake version
    if(CMAKE_VERSION VERSION_LESS CMAKE_MIN_VERSION)
        message(FATAL_ERROR "CMake version ${CMAKE_VERSION} is too old. Minimum required: ${CMAKE_MIN_VERSION}")
    endif()
    message(STATUS "✓ CMake ${CMAKE_VERSION} found")
endfunction()

# Function to validate fetched dependencies
function(validate_fetched_dependencies)
    # Validate neuriplo library (only fetched when local backends are enabled)
    if(NEURIPLO_INFER_ENABLE_LOCAL_BACKENDS)
        if(NOT DEFINED neuriplo_SOURCE_DIR)
            message(FATAL_ERROR "neuriplo library not found. This should be fetched automatically by CMake.")
        endif()
        message(STATUS "✓ neuriplo library found at ${neuriplo_SOURCE_DIR}")
    else()
        message(STATUS "✓ neuriplo library skipped (local backends disabled)")
    endif()
    
    # Validate VideoCapture library
    if(NOT DEFINED VideoCapture_SOURCE_DIR)
        message(FATAL_ERROR "VideoCapture library not found. This should be fetched automatically by CMake.")
    endif()
    message(STATUS "✓ VideoCapture library found at ${VideoCapture_SOURCE_DIR}")
endfunction()

# Note: KServe transport security (HTTPS/OpenSSL, gRPC TLS) is now owned by the
# neuriplo-kserve-client library, which reports its own HTTPS/gRPC status at
# configure time (e.g. "neuriplo-kserve-client: HTTPS enabled (...)").

# Function to validate all dependencies for this project
# Named validate_project_dependencies to avoid collision with neuriplo's
# validate_all_dependencies which gets loaded via FetchContent
function(validate_project_dependencies)
    message(STATUS "=== Validating Project Dependencies ===")

    validate_project_system_dependencies()
    validate_fetched_dependencies()

    message(STATUS "=== All Project Dependencies Validated Successfully ===")
endfunction()

# Function to check if we're in a Docker environment
function(is_docker_environment result)
    if(EXISTS "/.dockerenv")
        set(${result} TRUE PARENT_SCOPE)
    else()
        set(${result} FALSE PARENT_SCOPE)
    endif()
endfunction()

# Function to provide helpful setup instructions
function(print_setup_instructions)
    message(STATUS "=== Setup Instructions ===")
    message(STATUS "This project uses the neuriplo library for inference backend management.")
    message(STATUS "For inference backend setup, please refer to the neuriplo library documentation.")
    message(STATUS "")
    message(STATUS "System dependencies can be installed with:")
    message(STATUS "  sudo apt update && sudo apt install -y libopencv-dev libgoogle-glog-dev")
    message(STATUS "")
endfunction()

# Note: Inference backend validation (ONNX Runtime, TensorRT, LibTorch, etc.)
# should be handled by the neuriplo library, not this project.
# This project only validates its own dependencies and the fetched libraries. 
