#!/usr/bin/env bash
# PR #32316 "fix(mobile): Plan Mode shows the Build/Plan control again".
# Claim: obsolete. #33131 (521ac7c218) removed Plan mode from mobile, including the
# Labs -> Legacy -> Plan Mode preference and the composer's interaction-mode handler.
# The PR's chip is gated on both, so it has nothing left to restore.
# Static proof (native UI, no unit-testable surface). Read-only: no checkout, no writes.
# Run from the root of a pingdotgg/t3code clone: bash prove-pr-32316.sh
set -euo pipefail
trap 'rc=$?; [ "${PROOF_DONE:-0}" = 1 ] && exit 0; [ "$rc" -ne 0 ] && exit "$rc"; exit 1' EXIT

git fetch -q origin main

MAIN=$(git rev-parse FETCH_HEAD)
git fetch -q origin pull/32316/head
PR_HEAD=$(git rev-parse FETCH_HEAD)
SUPERSEDING=521ac7c218b3faa74281fb72fc36ce74a0951fb3 # #33131
COMPOSER=apps/mobile/src/features/threads/ThreadComposer.tsx
HOOK=apps/mobile/src/features/threads/use-legacy-plan-mode-enabled.ts
STATE=apps/mobile/src/state/legacy-plan-mode.ts
echo "main=$MAIN pr_head=$PR_HEAD"

failures=0
check() { # check <description> <command...>
  local what=$1; shift
  if "$@" >/dev/null 2>&1; then echo "PASS  $what"; else echo "FAIL  $what"; failures=$((failures + 1)); fi
}
absent_on_main() { ! git cat-file -e "$MAIN:$1"; }
no_match_on_main() { ! git grep -q -E "$1" "$MAIN" -- "$2"; }
pr_imports_hook() { git show "$PR_HEAD:$COMPOSER" | grep -q 'from "./use-legacy-plan-mode-enabled"'; }
pr_gates_on_pref_and_handler() {
  git show "$PR_HEAD:$COMPOSER" | grep -q 'legacyPlanModeEnabled && onUpdateInteractionMode'
}
deleted_by_superseding() {
  git show --diff-filter=D --name-only --format= "$SUPERSEDING" | grep -qx "$1"
}

check "#33131 merge commit ${SUPERSEDING:0:10} is on main" git merge-base --is-ancestor "$SUPERSEDING" "$MAIN"
check "PR head ThreadComposer imports useLegacyPlanModeState from $HOOK" pr_imports_hook
check "PR head shows the chip only when the Legacy Plan Mode pref is on and onUpdateInteractionMode exists" pr_gates_on_pref_and_handler
check "main no longer has $HOOK" absent_on_main "$HOOK"
check "main no longer has $STATE (the Legacy Plan Mode preference)" absent_on_main "$STATE"
check "#33131 deleted $HOOK" deleted_by_superseding "$HOOK"
check "#33131 deleted $STATE" deleted_by_superseding "$STATE"
check "main's mobile app has no onUpdateInteractionMode handler for the chip to call" \
  no_match_on_main 'onUpdateInteractionMode|useLegacyPlanModeState' apps/mobile
check "main's mobile composer state says plan mode is not offered (every send is build)" \
  git grep -q 'Plan mode is not offered to users' "$MAIN" -- apps/mobile/src/state/use-thread-composer-state.ts

if [ "$failures" -eq 0 ]; then
  echo "VERDICT: CLOSE (obsolete) - #33131 removed mobile Plan mode and the preference/handler this PR's control depends on"
  PROOF_DONE=1; exit 0
fi
echo "VERDICT: NOT PROVEN - $failures check(s) failed"
exit 1
