#!/usr/bin/env bash
set -euo pipefail

build_root=$1
src=$2
expected_result=$3
expected_breakdown=$4
expected_generated=${5:-}
forbidden_generated=${6:-}

cli_dir="$build_root/src/cli"
php2ast="$cli_dir/phpefc-php2ast"
phpast2phpir="$cli_dir/phpefc-phpast2phpir"
phpir2valair="$cli_dir/phpefc-phpir2valair"
valair_optimize="$cli_dir/phpefc-valair-optimize-typing"
valair2vala="$cli_dir/phpefc-valair2vala"

for tool in "$php2ast" "$phpast2phpir" "$phpir2valair" "$valair_optimize" "$valair2vala"; do
    if [ ! -x "$tool" ]; then
        echo "missing built tool: $tool" >&2
        exit 99
    fi
done

work=${MESON_TEST_TMPDIR:-}
if [ -z "$work" ]; then
    work=$(mktemp -d)
    trap 'rm -rf "$work"' EXIT
fi
mkdir -p "$work"

base="$work/typing-zval-breakdown"
phpast="$base.phpast"
phpir="$base.phpir"
valair="$base.valair"
afteropt="$base.afteropt.typing-zval2static"
vala="$base.vala"
stderr_file="$base.stderr"

"$php2ast" "$src" "$phpast" >/dev/null
"$phpast2phpir" "$phpast" "$phpir" >/dev/null
"$phpir2valair" "$phpir" "$valair" >/dev/null
"$valair_optimize" "$valair" "$afteropt" >/dev/null 2>"$stderr_file"
"$valair2vala" "$afteropt" "$vala" >/dev/null

result_marker="// phpefc optimizer typing-zval2static result: $expected_result"
breakdown_marker="// phpefc optimizer typing-zval2static breakdown: $expected_breakdown"

for file in "$afteropt" "$vala"; do
    if ! grep -a -Fq "$result_marker" "$file"; then
        echo "expected typing optimizer result marker in $file: $result_marker" >&2
        cat "$file" >&2
        exit 1
    fi
    if [ "$expected_breakdown" != "-" ]; then
        if ! grep -a -Fq "$breakdown_marker" "$file"; then
            echo "expected typing optimizer breakdown marker in $file: $breakdown_marker" >&2
            cat "$file" >&2
            exit 1
        fi
    elif grep -a -Fq "phpefc optimizer typing-zval2static breakdown:" "$file"; then
        echo "unexpected typing optimizer breakdown marker in $file" >&2
        cat "$file" >&2
        exit 1
    fi
done

if grep -a -q $'\033' "$afteropt" || grep -q $'\033' "$vala"; then
    echo "optimizer marker comments must not contain ANSI escape codes" >&2
    cat "$vala" >&2
    exit 1
fi

case "$expected_result" in
    0/*)
        if grep -Fq "WARNING: PERFORMANCE NOT OPTIMIZED THROUGH DYNAMIC ZVALS" "$stderr_file"; then
            echo "unexpected dynamic-Zval performance warning for fully optimized fixture" >&2
            cat "$stderr_file" >&2
            exit 1
        fi
        ;;
    *)
        warning="WARNING: PERFORMANCE NOT OPTIMIZED THROUGH DYNAMIC ZVALS ($expected_result)"
        if ! grep -Fq "$warning" "$stderr_file"; then
            echo "expected compatible warning without breakdown text: $warning" >&2
            cat "$stderr_file" >&2
            exit 1
        fi
        if grep -Fq "breakdown:" "$stderr_file" || grep -Fq "remaining " "$stderr_file"; then
            echo "typing warning should stay parser-compatible and not include breakdown text" >&2
            cat "$stderr_file" >&2
            exit 1
        fi
        ;;
esac

if [ -n "$expected_generated" ] && ! grep -Fq "$expected_generated" "$vala"; then
    echo "expected generated Vala snippet: $expected_generated" >&2
    cat "$vala" >&2
    exit 1
fi
if [ -n "$forbidden_generated" ] && grep -Fq "$forbidden_generated" "$vala"; then
    echo "forbidden generated Vala snippet still present: $forbidden_generated" >&2
    cat "$vala" >&2
    exit 1
fi

printf 'ok - typing-zval2static breakdown matches expected result for %s (%s; %s)\n' "$src" "$expected_result" "$expected_breakdown"
