#!/bin/zsh
# gt-open [sibling_id] [--cwd dir] [--cmd "command"] [--name title] [--ready-delay max_seconds] [--json]
# Opens a named tab in the window containing sibling_id (front window if omitted).
# Prints the new terminal's id on stdout — keep it; it is the handle for every other gt-* script.
# Readiness is polled, not slept: returns as soon as the new surface paints a
# non-blank, settled frame (shell prompt or TUI first paint), up to
# --ready-delay seconds (default 5). --no-wait skips the readiness poll.
set -euo pipefail

HERE="${0:A:h}"
SIBLING="" CWD="" CMD="" NAME="" READY_DELAY=5 JSON=0
while [[ $# -gt 0 ]]; do
  case "$1" in
    --cwd)  CWD="$2"; shift 2 ;;
    --cmd)  CMD="$2"; shift 2 ;;
    --name) NAME="$2"; shift 2 ;;
    --ready-delay) READY_DELAY="$2"; shift 2 ;;
    --no-wait) READY_DELAY=0; shift ;;
    --json) JSON=1; shift ;;
    -h|--help) sed -n '2,8p' "$0"; exit 0 ;;
    *) SIBLING="$1"; shift ;;
  esac
done
[[ -n "$NAME" ]] || NAME="gt: ${CMD:-shell}"
[[ "$READY_DELAY" == <->(|.<->) ]] || { echo "gt-open: --ready-delay must be a non-negative number" >&2; exit 2; }

json_escape() {
  local s="${1-}"
  s="${s//\\/\\\\}"
  s="${s//\"/\\\"}"
  s="${s//$'\n'/\\n}"
  s="${s//$'\r'/\\r}"
  s="${s//$'\t'/\\t}"
  printf '%s' "$s"
}

ID=$(osascript - "$SIBLING" "$CWD" "$CMD" "$NAME" "$READY_DELAY" <<'EOF'
on run argv
    set sibling to item 1 of argv
    set cwd to item 2 of argv
    set cmd to item 3 of argv
    set tabName to item 4 of argv
    tell application "Ghostty"
        -- pick target window: the one containing the sibling terminal, else front
        set w to front window
        if sibling is not "" then
            repeat with cw in windows
                repeat with t in terminals of cw
                    if id of t is sibling then
                        set w to cw
                        exit repeat
                    end if
                end repeat
            end repeat
        end if
        set conf to new surface configuration
        if cwd is not "" then set initial working directory of conf to cwd
        if cmd is not "" then set initial input of conf to (cmd & linefeed)
        set tb to new tab in w with configuration conf
        delay 0.35 -- let the surface resolve; shell readiness is polled by the caller
        set t to focused terminal of tb
        perform action ("set_tab_title:" & tabName) on t
        return id of t
    end tell
end run
EOF
)
# readiness poll: first non-blank frame that holds steady two polls in a row.
# Plain shells settle as soon as the prompt paints (usually well under 1.5s);
# animated TUIs never settle and fall through at the cap, which only means the
# caller should gt-wait --for their first known pattern anyway.
if [[ "$READY_DELAY" != "0" ]]; then
  typeset -F SECONDS
  START=$SECONDS PREV=""
  while (( SECONDS - START < READY_DELAY )); do
    FRAME=$("$HERE/gt-screen" "$ID" 2>/dev/null || true)
    if [[ -n "${FRAME//[[:space:]]/}" && "$FRAME" == "$PREV" ]]; then break; fi
    PREV="$FRAME"
    sleep 0.15
  done
fi

if [[ "$JSON" == "1" ]]; then
  CMD_PRESENT=false; [[ -n "$CMD" ]] && CMD_PRESENT=true
  printf '{"backend":"applescript","id":"%s","name":"%s","cwd":"%s","command_present":%s,"ready_delay":%s}\n' \
    "$(json_escape "$ID")" "$(json_escape "$NAME")" "$(json_escape "$CWD")" "$CMD_PRESENT" "$READY_DELAY"
else
  echo "$ID"
fi
