# matrix.conf - マトリックステスト定義
# 全組み合わせテスト（full プロファイル用）

# マトリックス軸定義
declare -a MATRIX_BACKENDS=("vm" "llvm")
declare -a MATRIX_PLUGIN_MODES=("dynamic" "static")
declare -a MATRIX_OPTIMIZATION_LEVELS=("debug" "release")
declare -a MATRIX_USING_MODES=("enabled" "disabled")

# 除外組み合わせ（実行不可能な組み合わせ）
MATRIX_EXCLUSIONS=(
    "vm:static"              # VMバックエンドは静的プラグイン非対応
    "llvm:dynamic:debug"     # LLVM+動的プラグインのデバッグモードは不安定
)

# 組み合わせ生成
generate_matrix_combinations() {
    local combinations=()

    for backend in "${MATRIX_BACKENDS[@]}"; do
        for plugin_mode in "${MATRIX_PLUGIN_MODES[@]}"; do
            for opt_level in "${MATRIX_OPTIMIZATION_LEVELS[@]}"; do
                for using_mode in "${MATRIX_USING_MODES[@]}"; do
                    local combo="${backend}:${plugin_mode}:${opt_level}:${using_mode}"

                    # 除外チェック
                    local excluded=false
                    for exclusion in "${MATRIX_EXCLUSIONS[@]}"; do
                        if echo "$combo" | grep -q "^$exclusion"; then
                            excluded=true
                            break
                        fi
                    done

                    if [ "$excluded" = false ]; then
                        combinations+=("$combo")
                    fi
                done
            done
        done
    done

    printf '%s\n' "${combinations[@]}"
}

# 環境設定適用
apply_matrix_config() {
    local combination="$1"

    IFS=':' read -ra PARTS <<< "$combination"
    local backend="${PARTS[0]}"
    local plugin_mode="${PARTS[1]}"
    local opt_level="${PARTS[2]}"
    local using_mode="${PARTS[3]}"

    echo "[INFO] Applying matrix config: $combination" >&2

    # バックエンド設定
    case "$backend" in
        "vm")
            export NYASH_BACKEND=""
            ;;
        "llvm")
            export NYASH_BACKEND="llvm"
            export NYASH_LLVM_USE_HARNESS=1
            ;;
    esac

    # プラグインモード設定
    case "$plugin_mode" in
        "dynamic")
            export NYASH_DISABLE_PLUGINS=0
            export SMOKES_PLUGIN_MODE="dynamic"
            ;;
        "static")
            export NYASH_DISABLE_PLUGINS=1
            export SMOKES_PLUGIN_MODE="static"
            ;;
    esac

    # 最適化レベル設定
    case "$opt_level" in
        "debug")
            export NYASH_CLI_VERBOSE=1
            export NYASH_DEBUG_FUEL="unlimited"
            export NYASH_OPTIMIZATION_LEVEL=0
            ;;
        "release")
            export NYASH_CLI_VERBOSE=0
            export NYASH_DEBUG_FUEL="10000"
            export NYASH_OPTIMIZATION_LEVEL=3
            ;;
    esac

    # using system設定
    case "$using_mode" in
        "enabled")
            export NYASH_ENABLE_USING=1
            export NYASH_RESOLVE_FIX_BRACES=1
            ;;
        "disabled")
            export NYASH_ENABLE_USING=0
            export NYASH_RESOLVE_FIX_BRACES=0
            ;;
    esac

    # マトリックス用調整
    export SMOKES_MATRIX_MODE=1
    export SMOKES_CURRENT_COMBINATION="$combination"
    export SMOKES_DEFAULT_TIMEOUT=90  # マトリックステストは時間がかかる
}

# マトリックステスト実行
run_matrix_test() {
    local test_script="$1"
    local combination="$2"

    echo "[INFO] Running matrix test: $test_script with $combination" >&2

    # 組み合わせ適用
    apply_matrix_config "$combination"

    # テストスクリプト実行
    local start_time end_time duration exit_code
    start_time=$(date +%s.%N)

    if timeout "${SMOKES_DEFAULT_TIMEOUT:-90}" bash "$test_script"; then
        exit_code=0
    else
        exit_code=$?
    fi

    end_time=$(date +%s.%N)
    duration=$(echo "$end_time - $start_time" | bc -l)

    # 結果記録
    echo "[RESULT] $test_script:$combination:$exit_code:${duration}s" >&2

    return $exit_code
}

# 全マトリックステスト実行
run_all_matrix_tests() {
    local test_pattern="${1:-*.sh}"
    local results_file="${2:-matrix_results.txt}"

    echo "[INFO] Starting matrix testing with pattern: $test_pattern" >&2

    local combinations
    mapfile -t combinations < <(generate_matrix_combinations)

    local total_tests=0
    local passed_tests=0
    local failed_tests=0

    # 結果ファイル初期化
    echo "# Matrix Test Results - $(date)" > "$results_file"
    echo "# Format: test_script:combination:exit_code:duration" >> "$results_file"

    # テストスクリプト検索
    local test_scripts
    mapfile -t test_scripts < <(find profiles/full/matrix -name "$test_pattern" -type f)

    for script in "${test_scripts[@]}"; do
        for combination in "${combinations[@]}"; do
            ((total_tests++))

            echo "[INFO] [$total_tests] Testing $script with $combination" >&2

            if run_matrix_test "$script" "$combination"; then
                ((passed_tests++))
                echo "PASS:$script:$combination:$(date)" >> "$results_file"
            else
                ((failed_tests++))
                echo "FAIL:$script:$combination:$(date)" >> "$results_file"
            fi
        done
    done

    # サマリー出力
    cat << EOF

===============================================
Matrix Test Summary
===============================================
Total combinations: ${#combinations[@]}
Total tests: $total_tests
Passed: $passed_tests
Failed: $failed_tests
Success rate: $(echo "scale=1; $passed_tests * 100 / $total_tests" | bc -l)%

Results saved to: $results_file
===============================================
EOF

    [ $failed_tests -eq 0 ]
}

# マトリックス情報表示
show_matrix_info() {
    echo "Available Matrix Combinations:"
    echo "=============================="

    local combinations
    mapfile -t combinations < <(generate_matrix_combinations)

    local count=1
    for combo in "${combinations[@]}"; do
        printf "%2d. %s\n" $count "$combo"
        ((count++))
    done

    echo ""
    echo "Total combinations: ${#combinations[@]}"
    echo ""
    echo "Matrix Axes:"
    echo "  Backends: ${MATRIX_BACKENDS[*]}"
    echo "  Plugin Modes: ${MATRIX_PLUGIN_MODES[*]}"
    echo "  Optimization: ${MATRIX_OPTIMIZATION_LEVELS[*]}"
    echo "  Using System: ${MATRIX_USING_MODES[*]}"
    echo ""
    echo "Exclusions:"
    for exclusion in "${MATRIX_EXCLUSIONS[@]}"; do
        echo "  - $exclusion"
    done
}

# 使用例
# source configs/matrix.conf
# show_matrix_info
# run_all_matrix_tests "*.sh" "results.txt"