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

if [[ $# -lt 2 ]]; then
  echo "Usage: ./tools/ab-profile.sh <current|legacy|partial-safe|partial-lite|auto-advance> <udid> [org]"
  exit 2
fi

profile="$1"
udid="$2"
org="${3:-ReBreak}"
serve_timeout_sec="${AB_SERVE_TIMEOUT_SEC:-240}"

if [[ ! "$serve_timeout_sec" =~ ^[0-9]+$ ]] || [[ "$serve_timeout_sec" -le 0 ]]; then
  echo "AB_SERVE_TIMEOUT_SEC must be a positive integer (seconds)."
  exit 2
fi

export REBREAK_MB2_SERVE_TIMEOUT_SEC="$serve_timeout_sec"

case "$profile" in
  current)
    export REBREAK_MB2_DYNAMIC_METADATA=0
    export REBREAK_MB2_REMOVE_ITEMS_NOT_RESTORED=0
    export REBREAK_SEND_SYNC_DID_FINISH=0
    unset REBREAK_CC_UI_COMPLETE
    unset REBREAK_CC_POST_SETUP_PROFILE
    unset REBREAK_CC_CONFIGURATION_WAS_APPLIED
    unset REBREAK_MB2_TL_VERBATIM_META
    unset REBREAK_CC_SKIPSETUP_PROFILE
    unset REBREAK_CC_SKIPSETUP_EXTRA
    unset REBREAK_CC_AUTO_ADVANCE
    ;;
  legacy)
    export REBREAK_MB2_DYNAMIC_METADATA=1
    export REBREAK_MB2_REMOVE_ITEMS_NOT_RESTORED=1
    export REBREAK_SEND_SYNC_DID_FINISH=1
    unset REBREAK_CC_UI_COMPLETE
    unset REBREAK_CC_POST_SETUP_PROFILE
    unset REBREAK_CC_CONFIGURATION_WAS_APPLIED
    unset REBREAK_MB2_TL_VERBATIM_META
    unset REBREAK_CC_SKIPSETUP_PROFILE
    unset REBREAK_CC_SKIPSETUP_EXTRA
    unset REBREAK_CC_AUTO_ADVANCE
    ;;
  partial-safe)
    # Zielprofil: Settings/Account wie current behalten, aber den
    # "teilweise eingerichtet"-Checkpoint erzwingen.
    export REBREAK_MB2_DYNAMIC_METADATA=0
    export REBREAK_MB2_REMOVE_ITEMS_NOT_RESTORED=0
    export REBREAK_SEND_SYNC_DID_FINISH=0
    export REBREAK_CC_UI_COMPLETE=0
    export REBREAK_CC_POST_SETUP_PROFILE=0
    export REBREAK_CC_CONFIGURATION_WAS_APPLIED=1
    unset REBREAK_MB2_TL_VERBATIM_META
    unset REBREAK_CC_SKIPSETUP_PROFILE
    unset REBREAK_CC_SKIPSETUP_EXTRA
    unset REBREAK_CC_AUTO_ADVANCE
    ;;
  partial-lite)
    # Entschaerft: nur "UI nicht komplett" signalisieren, aber keine
    # Post-Setup/Applied-Ruecknahme (vermeidet Registration-Branch).
    export REBREAK_MB2_DYNAMIC_METADATA=0
    export REBREAK_MB2_REMOVE_ITEMS_NOT_RESTORED=0
    export REBREAK_SEND_SYNC_DID_FINISH=0
    export REBREAK_CC_UI_COMPLETE=0
    export REBREAK_CC_POST_SETUP_PROFILE=1
    export REBREAK_CC_CONFIGURATION_WAS_APPLIED=1
    unset REBREAK_MB2_TL_VERBATIM_META
    unset REBREAK_CC_SKIPSETUP_PROFILE
    unset REBREAK_CC_SKIPSETUP_EXTRA
    unset REBREAK_CC_AUTO_ADVANCE
    ;;
  auto-advance)
    # Hypothese 2026-05-28: AutoAdvanceSetup=true (statt false) lässt iOS
    # den Setup Assistant automatisch durchlaufen → kein sichtbarer
    # Language/Region Screen, direkt zu Kamera wie bei TL.
    # Manifest.db wird generiert (korrekte CC-Größe, nicht TL-hardcoded 412B).
    export REBREAK_MB2_DYNAMIC_METADATA=0
    export REBREAK_MB2_REMOVE_ITEMS_NOT_RESTORED=0
    export REBREAK_SEND_SYNC_DID_FINISH=0
    export REBREAK_CC_AUTO_ADVANCE=1
    unset REBREAK_CC_UI_COMPLETE
    unset REBREAK_CC_POST_SETUP_PROFILE
    unset REBREAK_CC_CONFIGURATION_WAS_APPLIED
    unset REBREAK_MB2_TL_VERBATIM_META
    unset REBREAK_MB2_TL_VERBATIM_MANIFEST_DB
    unset REBREAK_CC_SKIPSETUP_PROFILE
    unset REBREAK_CC_SKIPSETUP_EXTRA
    ;;
  *)
    echo "Unknown profile: $profile"
    echo "Allowed: current, legacy, partial-safe, partial-lite, auto-advance"
    exit 2
    ;;
esac

echo "Profile: $profile"
echo "UDID: $udid"
echo "ORG: $org"
echo "REBREAK_MB2_DYNAMIC_METADATA=$REBREAK_MB2_DYNAMIC_METADATA"
echo "REBREAK_MB2_REMOVE_ITEMS_NOT_RESTORED=$REBREAK_MB2_REMOVE_ITEMS_NOT_RESTORED"
echo "REBREAK_SEND_SYNC_DID_FINISH=$REBREAK_SEND_SYNC_DID_FINISH"
echo "REBREAK_CC_UI_COMPLETE=${REBREAK_CC_UI_COMPLETE:-<unset>}"
echo "REBREAK_CC_POST_SETUP_PROFILE=${REBREAK_CC_POST_SETUP_PROFILE:-<unset>}"
echo "REBREAK_CC_CONFIGURATION_WAS_APPLIED=${REBREAK_CC_CONFIGURATION_WAS_APPLIED:-<unset>}"
echo "REBREAK_CC_AUTO_ADVANCE=${REBREAK_CC_AUTO_ADVANCE:-<unset (default=true)>}"
echo "REBREAK_MB2_SERVE_TIMEOUT_SEC=$REBREAK_MB2_SERVE_TIMEOUT_SEC"

echo "Building binary..."
make build

echo "Running supervise..."
./bin/rebreak-supervise-magic supervise --force -udid "$udid" -org "$org" -yes -v
