#!/bin/bash
# llvm_exe_runner.sh - Shared helpers for LLVM EXE parity smokes (integration)
#
# SSOT goals:
# - One place for "LLVM available?" SKIP logic
# - One place for "required plugins are dlopen-able" gating + conditional build-all
# - One place for "build_llvm.sh → run exe → numeric output compare"
#
# This file is meant to be sourced from smoke scripts that already source:
#   tools/smokes/v2/lib/test_runner.sh

set -uo pipefail

llvm_exe_preflight_or_skip() {
  if ! command -v llvm-config-18 &>/dev/null; then
    test_skip "llvm-config-18 not found"
    return 1
  fi

  if ! "$NYASH_BIN" --help 2>&1 | grep -q "llvm"; then
    test_skip "hakorune --backend llvm not available"
    return 1
  fi

  if ! python3 -c "import llvmlite" 2>/dev/null; then
    test_skip "Python llvmlite not found"
    return 1
  fi

  return 0
}

llvm_exe_check_plugins() {
  # Args: repeated triples: <DisplayName> <SoPath> <CrateName>
  python3 - "$@" <<'PY'
import ctypes
import os
import sys

args = sys.argv[1:]
if len(args) % 3 != 0:
    print(f"[internal] expected triples, got {len(args)} args")
    sys.exit(2)

failures = []
for i in range(0, len(args), 3):
    display, path, _crate = args[i], args[i + 1], args[i + 2]
    if not os.path.isfile(path):
        failures.append(f"[plugin/missing] {display}: {path}")
        continue
    try:
        ctypes.CDLL(path)
    except Exception as e:  # noqa: BLE001
        failures.append(f"[plugin/dlopen] {display}: {path} ({e})")

if failures:
    print("\n".join(failures))
    sys.exit(1)
print("OK")
PY
}

llvm_exe_ensure_plugins_or_fail() {
  # Uses global array LLVM_REQUIRED_PLUGINS (triples encoded as "Display|SoPath|CrateName")
  if ! declare -p LLVM_REQUIRED_PLUGINS >/dev/null 2>&1; then
    return 0
  fi
  if [ "${#LLVM_REQUIRED_PLUGINS[@]}" -eq 0 ]; then
    return 0
  fi

  local -a triples=()
  local -a crate_names=()
  local CHECK_OUTPUT
  local item display so_path crate_name

  for item in "${LLVM_REQUIRED_PLUGINS[@]}"; do
    IFS='|' read -r display so_path crate_name <<<"$item"
    if [ -z "${display:-}" ] || [ -z "${so_path:-}" ] || [ -z "${crate_name:-}" ]; then
      echo "[FAIL] Invalid LLVM_REQUIRED_PLUGINS entry: '$item'"
      return 1
    fi
    triples+=("$display" "$so_path" "$crate_name")
    crate_names+=("$crate_name")
  done

  echo "[INFO] Checking plugin artifacts (LLVM EXE)"
  if CHECK_OUTPUT=$(llvm_exe_check_plugins "${triples[@]}" 2>&1); then
    return 0
  fi

  echo "$CHECK_OUTPUT"
  echo "[INFO] Missing/broken plugin detected, running build-all"

  local build_log="${LLVM_PLUGIN_BUILD_LOG:-/tmp/llvm_exe_plugin_build.log}"
  if ! bash "$NYASH_ROOT/tools/plugins/build-all.sh" "${crate_names[@]}" >"$build_log" 2>&1; then
    echo "[FAIL] tools/plugins/build-all.sh failed"
    tail -n 80 "$build_log"
    return 1
  fi

  if ! CHECK_OUTPUT=$(llvm_exe_check_plugins "${triples[@]}" 2>&1); then
    echo "$CHECK_OUTPUT"
    echo "[FAIL] Plugin artifacts still missing or unloadable after build-all"
    tail -n 80 "$build_log"
    return 1
  fi

  return 0
}

llvm_exe_build_and_run_numeric_smoke() {
  # Required globals:
  # - INPUT_HAKO
  # - OUTPUT_EXE
  # - EXPECTED (multiline)
  # - EXPECTED_LINES (number of numeric lines to compare)
  #
  # Optional:
  # - LLVM_BUILD_LOG
  # - RUN_TIMEOUT_SECS

  if [ -z "${INPUT_HAKO:-}" ] || [ -z "${OUTPUT_EXE:-}" ] || [ -z "${EXPECTED:-}" ]; then
    echo "[FAIL] llvm_exe_build_and_run_numeric_smoke: missing INPUT_HAKO/OUTPUT_EXE/EXPECTED"
    return 1
  fi
  if [ -z "${EXPECTED_LINES:-}" ]; then
    echo "[FAIL] llvm_exe_build_and_run_numeric_smoke: missing EXPECTED_LINES"
    return 1
  fi

  mkdir -p "$(dirname "$OUTPUT_EXE")"

  echo "[INFO] Building: $INPUT_HAKO → $OUTPUT_EXE"

  local build_log="${LLVM_BUILD_LOG:-/tmp/llvm_exe_build.log}"
  if ! env NYASH_DISABLE_PLUGINS=0 "$NYASH_ROOT/tools/build_llvm.sh" "$INPUT_HAKO" -o "$OUTPUT_EXE" 2>&1 | tee "$build_log"; then
    echo "[FAIL] build_llvm.sh failed"
    tail -n 80 "$build_log"
    return 1
  fi

  if [ ! -x "$OUTPUT_EXE" ]; then
    echo "[FAIL] Executable not created or not executable: $OUTPUT_EXE"
    ls -la "$OUTPUT_EXE" 2>/dev/null || echo "File does not exist"
    return 1
  fi

  echo "[INFO] Executing: $OUTPUT_EXE"

  set +e
  local output
  output=$(timeout "${RUN_TIMEOUT_SECS:-10}" env NYASH_DISABLE_PLUGINS=0 "$OUTPUT_EXE" 2>&1)
  local exit_code=$?
  set -e

  if [ "$exit_code" -ne 0 ]; then
    echo "[FAIL] Execution failed with exit code $exit_code"
    echo "$output" | tail -n 80
    return 1
  fi

  local clean
  clean=$(printf "%s\n" "$output" | grep -v '^\[' | grep -E '^-?[0-9]+$' | head -n "$EXPECTED_LINES" | tr -d '\r')

  echo "[INFO] CLEAN output:"
  echo "$clean"

  if [ "$clean" = "$EXPECTED" ]; then
    return 0
  fi

  echo "[FAIL] Output mismatch"
  echo "[INFO] Raw output (tail):"
  echo "$output" | tail -n 80
  echo "[INFO] Expected:"
  printf "%s\n" "$EXPECTED"
  return 1
}
