#!/bin/bash
# Lia Desktop Launcher
# Handles first-run setup, auto-updates, and launch

set -e

APP_DIR="$HOME/.lia-desktop"
VENV_DIR="$APP_DIR/venv"
APP_FILE="$APP_DIR/lia_elegant.py"
LOG_FILE="$APP_DIR/launcher.log"
API_BASE="https://www.trylia.ai"

# Detect architecture
ARCH=$(uname -m)

log() {
    echo "$(date '+%Y-%m-%d %H:%M:%S') - $1" >> "$LOG_FILE"
    echo "$1"
}

show_notification() {
    osascript -e "display notification \"$1\" with title \"Lia\""
}

check_python() {
    if command -v python3 &> /dev/null; then
        PYTHON="python3"
    elif command -v python &> /dev/null; then
        PYTHON="python"
    else
        osascript -e 'display dialog "Python 3 is required but not installed.\n\nPlease install Python from python.org or via Homebrew:\n\nbrew install python3" buttons {"OK"} default button "OK" with title "Lia - Python Required" with icon caution'
        exit 1
    fi
}

first_run_setup() {
    log "First run - setting up Lia..."
    log "Architecture: $ARCH"
    
    # Show progress dialog
    osascript -e 'display dialog "Setting up Lia for first use...\n\nThis will take about 1-2 minutes." buttons {"OK"} default button "OK" giving up after 3 with title "Lia Setup"' &
    
    mkdir -p "$APP_DIR"
    
    # Download latest app
    log "Downloading Lia..."
    curl -sL "$API_BASE/api/lia-desktop/latest" -o "$APP_FILE"
    
    # Create virtual environment with correct architecture
    log "Creating virtual environment..."
    if [ "$ARCH" = "arm64" ]; then
        # Force ARM64 architecture on Apple Silicon
        arch -arm64 $PYTHON -m venv "$VENV_DIR"
    else
        $PYTHON -m venv "$VENV_DIR"
    fi
    
    # Install dependencies
    log "Installing dependencies (this may take a minute)..."
    source "$VENV_DIR/bin/activate"
    
    pip install --upgrade pip -q
    pip install PyQt6 openai-whisper requests numpy sounddevice -q
    
    deactivate
    
    log "Setup complete!"
    show_notification "Setup complete! Lia is ready."
}

check_for_updates() {
    log "Checking for updates..."
    
    # Get current version from local file
    if [ -f "$APP_FILE" ]; then
        LOCAL_VERSION=$(grep "^VERSION" "$APP_FILE" | head -1 | cut -d'"' -f2)
    else
        LOCAL_VERSION="0.0.0"
    fi
    
    # Get server version
    SERVER_VERSION=$(curl -s "$API_BASE/api/lia-desktop/version" | grep -o '"version":"[^"]*"' | cut -d'"' -f4)
    
    if [ -z "$SERVER_VERSION" ]; then
        log "Could not check server version, continuing with local"
        return
    fi
    
    log "Local: $LOCAL_VERSION, Server: $SERVER_VERSION"
    
    if [ "$LOCAL_VERSION" != "$SERVER_VERSION" ]; then
        log "Updating from $LOCAL_VERSION to $SERVER_VERSION..."
        show_notification "Updating to v$SERVER_VERSION..."
        curl -sL "$API_BASE/api/lia-desktop/latest" -o "$APP_FILE"
        log "Update complete!"
    fi
}

run_app() {
    log "Launching Lia..."
    source "$VENV_DIR/bin/activate"
    cd "$APP_DIR"
    $PYTHON "$APP_FILE" 2>> "$LOG_FILE"
}

# Main
mkdir -p "$APP_DIR"
log "=== Lia Launcher Started ==="

check_python

if [ ! -f "$VENV_DIR/bin/activate" ] || [ ! -f "$APP_FILE" ]; then
    first_run_setup
else
    check_for_updates
fi

run_app
