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

CALL_ID="${TUPLE_TRIGGER_CALL_ID:?sidekick-claude-desktop requires TUPLE_TRIGGER_CALL_ID}"
: "${TUPLE_TRIGGER_RECORDING_ID:?sidekick-claude-desktop requires TUPLE_TRIGGER_RECORDING_ID}"

# TUPLE_BIN="$HOME/bin/tuple"
# TUPLE_DESKTOP_WORKSPACE_ROOT="$HOME/Developer/tuple-calls"
if [ -n "${TUPLE_DESKTOP_WORKSPACE_ROOT:-}" ]; then
    WORKDIR="${TUPLE_DESKTOP_WORKSPACE_ROOT}"
else
    WORKDIR="${HOME}/.tuple/tuple-calls"
fi

if [ -n "${TUPLE_BIN:-}" ]; then
    if ! TUPLE_CLI="$(command -v "${TUPLE_BIN}")"; then
        echo "sidekick-claude-desktop: TUPLE_BIN is not executable: ${TUPLE_BIN}" >&2
        exit 127
    fi
elif TUPLE_CLI="$(command -v tuple)"; then
    :
elif [ -x "/Applications/Tuple.app/Contents/SharedSupport/bin/tuple" ]; then
    TUPLE_CLI="/Applications/Tuple.app/Contents/SharedSupport/bin/tuple"
else
    echo "sidekick-claude-desktop: could not find the Tuple CLI; set TUPLE_BIN to its path" >&2
    exit 127
fi

urlencode() {
    local LC_ALL=C
    local string="${1}"
    local encoded=""
    local pos char byte

    for ((pos = 0; pos < ${#string}; pos++)); do
        char="${string:${pos}:1}"
        case "${char}" in
            [a-zA-Z0-9.~_-]) encoded+="${char}" ;;
            *)
                printf -v byte '%d' "'${char}"
                printf -v encoded '%s%%%02X' "${encoded}" "$((byte & 255))"
                ;;
        esac
    done

    printf '%s' "${encoded}"
}

mkdir -p "${WORKDIR}"

PROMPT="$("${TUPLE_CLI}" connect prompt --call "${CALL_ID}")"
DEEPLINK="claude://code/new?folder=$(urlencode "${WORKDIR}")&prompt=$(urlencode "${PROMPT}")"

if [ "${SIDEKICK_CLAUDE_DESKTOP_DRY_RUN:-}" = "1" ]; then
    echo "sidekick-claude-desktop: dry run — would open ${DEEPLINK}"
    exit 0
fi

echo "sidekick-claude-desktop: opening Claude Desktop"
open -a "Claude" "${DEEPLINK}"
