#!/bin/bash
# ===== SSOT: Smoke Environment Configuration =====
# All environment variable configurations for Phase 131+ smoke tests
#
# This file centralizes environment variable settings to:
# - Prevent "sparrow" bugs (scattered duplicates across scripts)
# - Provide single source of truth (SSOT) for smoke test environment
# - Enable easy modification of defaults across all tests
#
# Usage:
#   source "$(dirname "$0")/../lib/env.sh"
#   setup_smoke_env [mode]  # mode: dev|integration|quick (default: dev)

set -uo pipefail

# ============================================================================
# JoinIR Development Mode (Phase 131+)
# ============================================================================
# Required for dev-only fixtures that use normalized shadow control flow.
# Without these, LLVM EXE emission may freeze on dev-only patterns.
#
# Default: enabled (all Phase 131+ fixtures require this)
export NYASH_JOINIR_DEV="${NYASH_JOINIR_DEV:-1}"
export HAKO_JOINIR_STRICT="${HAKO_JOINIR_STRICT:-1}"

# ============================================================================
# LLVM Features
# ============================================================================
# LLVM harness execution (Python llvmlite backend)
export NYASH_LLVM_USE_HARNESS="${NYASH_LLVM_USE_HARNESS:-1}"

# LLVM backend selection (crate|python|auto)
export NYASH_LLVM_BACKEND="${NYASH_LLVM_BACKEND:-crate}"

# LLVM verification (0=off, 1=on)
export NYASH_LLVM_VERIFY="${NYASH_LLVM_VERIFY:-0}"
export NYASH_LLVM_VERIFY_IR="${NYASH_LLVM_VERIFY_IR:-0}"

# ============================================================================
# Tmpdir EXDEV Mitigation (for build_llvm.sh)
# ============================================================================
# TARGET_TMPDIR allows smoke scripts to override TMPDIR location.
# This prevents "Invalid cross-device link" errors when /tmp is on different
# filesystem than target directory.
#
# Default: use current directory (workspace target/release/deps)
export TARGET_TMPDIR="${TARGET_TMPDIR:-.}"

# ============================================================================
# Plugin Loader Strategy
# ============================================================================
# NYASH_LOAD_NY_PLUGINS: Load .hako-based plugins from nyash.toml
# Default: 0 (use .so plugins)
export NYASH_LOAD_NY_PLUGINS="${NYASH_LOAD_NY_PLUGINS:-0}"

# NYASH_DISABLE_PLUGINS: Disable all plugins except core builtins
# Default: 0 (plugins enabled)
export NYASH_DISABLE_PLUGINS="${NYASH_DISABLE_PLUGINS:-0}"

# ============================================================================
# Parser Features
# ============================================================================
# Stage 3 parser features (Phase 251+)
export NYASH_FEATURES="${NYASH_FEATURES:-stage3}"

# Using system (namespace/package imports)
export NYASH_ENABLE_USING="${NYASH_ENABLE_USING:-1}"
export HAKO_ENABLE_USING="${HAKO_ENABLE_USING:-1}"

# Using file imports
export NYASH_ALLOW_USING_FILE="${NYASH_ALLOW_USING_FILE:-1}"
export HAKO_ALLOW_USING_FILE="${HAKO_ALLOW_USING_FILE:-1}"

# Using AST mode
export NYASH_USING_AST="${NYASH_USING_AST:-1}"

# ============================================================================
# Debug Features (Optional, Gated by SMOKE_DEBUG)
# ============================================================================
if [ "${SMOKE_DEBUG:-0}" = "1" ]; then
    # Verbose CLI output
    export NYASH_CLI_VERBOSE=1

    # Trace execution
    export HAKO_TRACE_EXECUTION=1
    export HAKO_VERIFY_SHOW_LOGS=1

    # Unlimited debug fuel
    export NYASH_DEBUG_FUEL="unlimited"
else
    # Production defaults (quiet)
    export NYASH_CLI_VERBOSE="${NYASH_CLI_VERBOSE:-0}"
    export HAKO_TRACE_EXECUTION="${HAKO_TRACE_EXECUTION:-0}"
    export HAKO_VERIFY_SHOW_LOGS="${HAKO_VERIFY_SHOW_LOGS:-0}"
    export NYASH_DEBUG_FUEL="${NYASH_DEBUG_FUEL:-10000}"
fi

# ============================================================================
# Silent Tags (Reduce Log Noise)
# ============================================================================
export HAKO_SILENT_TAGS="${HAKO_SILENT_TAGS:-1}"

# ============================================================================
# Mode-Specific Configuration
# ============================================================================
# Allows smoke scripts to request different environment modes
setup_smoke_env() {
    local mode="${1:-dev}"

    case "$mode" in
        dev)
            # Development mode: verbose, unlimited fuel
            export NYASH_CLI_VERBOSE=1
            export NYASH_DEBUG_FUEL="unlimited"
            export NYASH_JOINIR_DEV=1
            export HAKO_JOINIR_STRICT=1
            ;;

        integration)
            # Integration mode: JoinIR dev enabled, moderate verbosity
            export NYASH_JOINIR_DEV=1
            export HAKO_JOINIR_STRICT=1
            export NYASH_CLI_VERBOSE="${NYASH_CLI_VERBOSE:-0}"
            export NYASH_DEBUG_FUEL="${NYASH_DEBUG_FUEL:-10000}"
            ;;

        quick)
            # Quick mode: minimal logging, fast execution
            export NYASH_CLI_VERBOSE=0
            export NYASH_DEBUG_FUEL="10000"
            export HAKO_SILENT_TAGS=1
            ;;

        *)
            echo "[WARN] setup_smoke_env: unknown mode '$mode', using dev defaults"
            setup_smoke_env dev
            ;;
    esac
}

# ============================================================================
# Validation Helpers
# ============================================================================
validate_env_setup() {
    local warnings=0

    # Check JoinIR dev mode (required for Phase 131+)
    if [ "${NYASH_JOINIR_DEV:-0}" != "1" ]; then
        echo "[WARN] NYASH_JOINIR_DEV not enabled (may fail on Phase 131+ fixtures)"
        warnings=$((warnings + 1))
    fi

    if [ "${HAKO_JOINIR_STRICT:-0}" != "1" ]; then
        echo "[WARN] HAKO_JOINIR_STRICT not enabled (may fail on Phase 131+ fixtures)"
        warnings=$((warnings + 1))
    fi

    # Check LLVM harness (required for LLVM EXE smokes)
    if [ "${NYASH_LLVM_USE_HARNESS:-0}" != "1" ]; then
        echo "[INFO] NYASH_LLVM_USE_HARNESS not enabled (LLVM EXE tests will be skipped)"
    fi

    return $warnings
}

# Show active environment configuration
show_smoke_env() {
    cat <<EOF
[INFO] Smoke Environment Configuration (SSOT: lib/env.sh)
  JoinIR Dev:          NYASH_JOINIR_DEV=${NYASH_JOINIR_DEV}
  JoinIR Strict:       HAKO_JOINIR_STRICT=${HAKO_JOINIR_STRICT}
  LLVM Harness:        NYASH_LLVM_USE_HARNESS=${NYASH_LLVM_USE_HARNESS}
  LLVM Backend:        NYASH_LLVM_BACKEND=${NYASH_LLVM_BACKEND}
  Plugins:             NYASH_DISABLE_PLUGINS=${NYASH_DISABLE_PLUGINS}
  Debug Fuel:          NYASH_DEBUG_FUEL=${NYASH_DEBUG_FUEL}
  Verbose:             NYASH_CLI_VERBOSE=${NYASH_CLI_VERBOSE}
EOF
}

# Auto-validate on source (optional, gated by flag)
if [ "${SMOKE_ENV_VALIDATE:-0}" = "1" ]; then
    validate_env_setup
fi
