#!/bin/bash
# SoranaFlow Debug Launcher v1.1
# Double-click to run SoranaFlow with full logging.
# When SoranaFlow quits, a log file is saved to ~/Downloads.

TIMESTAMP=$(date +"%Y%m%d_%H%M%S")
LOGFILE="$HOME/Downloads/SoranaFlow-Debug-${TIMESTAMP}.log"

# Find SoranaFlow.app — check common locations
SORANA=""
for candidate in \
    "/Applications/SoranaFlow.app" \
    "$HOME/Applications/SoranaFlow.app" \
    "$(dirname "$0")/../../../SoranaFlow.app"; do
    if [ -d "$candidate" ]; then
        SORANA="$candidate"
        break
    fi
done

if [ -z "$SORANA" ]; then
    osascript -e 'display dialog "SoranaFlow.app not found.\nPlease install it in /Applications first." buttons {"OK"} default button "OK" with icon stop with title "SoranaFlow Debug"'
    exit 1
fi

BINARY="$SORANA/Contents/MacOS/SoranaFlow"

# Write header
{
    echo "═══════════════════════════════════════════════════"
    echo "  SoranaFlow Debug Report"
    echo "═══════════════════════════════════════════════════"
    echo "Date:      $(date)"
    echo "macOS:     $(sw_vers -productVersion) (Build: $(sw_vers -buildVersion))"
    echo "Model:     $(sysctl -n hw.model 2>/dev/null)"
    echo "Chip:      $(sysctl -n machdep.cpu.brand_string 2>/dev/null || echo 'unknown')"
    echo "ARM64:     $(sysctl -n hw.optional.arm64 2>/dev/null && echo 'Yes' || echo 'No/unknown')"
    echo "RAM:       $(( $(sysctl -n hw.memsize 2>/dev/null) / 1073741824 )) GB"
    echo "App:       $SORANA"
    echo "Binary:    $(file -b "$BINARY" 2>/dev/null)"
    echo "Version:   $(/usr/libexec/PlistBuddy -c 'Print CFBundleShortVersionString' "$SORANA/Contents/Info.plist" 2>/dev/null || echo 'unknown')"
    echo "Build:     $(/usr/libexec/PlistBuddy -c 'Print CFBundleVersion' "$SORANA/Contents/Info.plist" 2>/dev/null || echo 'unknown')"
    echo ""
    echo "── Audio Devices ──"
    system_profiler SPAudioDataType 2>/dev/null | grep -E "^\s+(.*:)" | head -30
    echo ""
    echo "── Default Output ──"
    coreaudiod_info=$(system_profiler SPAudioDataType 2>/dev/null | grep -A2 "Default Output" | head -3)
    echo "${coreaudiod_info:-N/A}"
    echo ""
    echo "── Disk Info ──"
    df -h / 2>/dev/null | tail -1
    echo ""
    echo "═══════════════════════════════════════════════════"
    echo ""
} > "$LOGFILE"

# Launch SoranaFlow, capture all output
"$BINARY" >> "$LOGFILE" 2>&1
EXIT_CODE=$?

# Write footer
{
    echo ""
    echo "═══════════════════════════════════════════════════"
    echo "  SoranaFlow exited with code: $EXIT_CODE"
    if [ $EXIT_CODE -eq 0 ]; then
        echo "  (Clean exit)"
    elif [ $EXIT_CODE -eq 143 ]; then
        echo "  (SIGTERM — force quit or system shutdown, settings may not be saved)"
    elif [ $EXIT_CODE -eq 137 ]; then
        echo "  (SIGKILL — killed by system, possible OOM)"
    elif [ $EXIT_CODE -eq 139 ]; then
        echo "  (SIGSEGV — crash)"
    elif [ $EXIT_CODE -eq 134 ]; then
        echo "  (SIGABRT — assertion failure)"
    else
        echo "  (Unexpected exit)"
    fi
    echo "  Log saved: $LOGFILE"
    echo "  $(date)"
    echo "═══════════════════════════════════════════════════"
} >> "$LOGFILE"

# Notify user
osascript -e "display dialog \"Debug log saved to Downloads folder:\\n\\nSoranaFlow-Debug-${TIMESTAMP}.log\\n\\nPlease send this file to the developer.\" buttons {\"Open in Finder\", \"OK\"} default button \"OK\" with title \"SoranaFlow Debug\"" \
    -e 'if button returned of result is "Open in Finder" then tell application "Finder" to reveal POSIX file "'"$LOGFILE"'"'
