#!/bin/zsh -f

sc_present_installed() {
	if [[ ${SUPERCODE_CLAUDE_INSTALL_PRESENTATION:-native} == none ]]; then
		return 0
	fi

	if [[ -z ${sc_pid:-} ]]; then
		if /usr/bin/open "$sc_app"; then
			sc_telemetry_track 'extension.installation_activation_selected' 'action=launch'
			print -r -- 'Claude launch requested.'
		else
			sc_telemetry_track 'extension.installation_presentation_failed' 'reason_code=application_launch_failed'
			/usr/bin/osascript -e \
				'display alert "Claude could not be opened automatically" message "Please open Claude Desktop manually to activate Supercode." as critical buttons {"OK"} default button "OK"' \
				>/dev/null 2>&1 || print -r -- 'Supercode installed. Open Claude Desktop manually to activate it.'
		fi
		return 0
	fi

	local sc_action
	if ! sc_action=$(/usr/bin/osascript -e \
		'display alert "Supercode installed" message "Restart Claude to activate the Supercode Extension." as informational buttons {"Later", "Restart Claude"} default button "Restart Claude"'); then
		sc_telemetry_track 'extension.installation_presentation_failed' 'reason_code=presentation_failed'
		print -r -- 'Supercode installed. Quit Claude completely and reopen it to activate Supercode.'
		return 0
	fi
	if [[ "$sc_action" != 'button returned:Restart Claude' ]]; then
		sc_telemetry_track 'extension.installation_activation_selected' 'action=later'
		return 0
	fi
	sc_telemetry_track 'extension.installation_activation_selected' 'action=restart'

	/usr/bin/nohup /bin/zsh -f "$sc_root/recover-v1" \
		--app "$sc_app" \
		--pid "$sc_pid" \
		--storage-root "$sc_root" \
		--bootstrap-namespace "$sc_bootstrap_namespace" \
		--user-data-dir "$sc_user_data" \
		--activation pending \
		--installation-attempt-id "$sc_installation_attempt_id" \
		</dev/null >/dev/null 2>&1 &!
	print -r -- 'Claude restart requested.'
}
