#!/bin/bash
# Quick Start Script for My Sport Manager Flutter Android Testing
# Run this from the dev machine before testing on Android phone

set -e

# === Environment Setup ===
export PATH="/mnt/ssd_data/flutter/bin:$PATH"
export JAVA_HOME=/usr/lib/jvm/java-21-openjdk-arm64
export ANDROID_HOME=/mnt/ssd_data/android-sdk

echo "🔧 Environment variables set:"
echo "  PATH includes Flutter: $PATH"
echo "  JAVA_HOME: $JAVA_HOME"
echo "  ANDROID_HOME: $ANDROID_HOME"
echo ""

# === Check Flutter ===
echo "📱 Checking Flutter..."
flutter --version
echo ""

# === Check Java ===
echo "☕ Checking Java..."
java -version
echo ""

# === Check Connected Devices ===
echo "📲 Checking connected devices..."
flutter devices
echo ""

# === Get Machine LAN IP ===
echo "🌐 Your LAN IP addresses:"
hostname -I
echo ""
echo "⚠️  Use one of the above IPs to update app_constants.dart DevConfig.localApiUrl"
echo ""

# === Prompt for IP ===
read -p "Enter your LAN IP (e.g., 192.168.1.100): " LAN_IP

if [ -z "$LAN_IP" ]; then
  echo "❌ No IP provided. Exiting."
  exit 1
fi

# === Update app_constants.dart ===
echo ""
echo "🔄 Updating app_constants.dart with your LAN IP..."
CONST_FILE="/mnt/ssd_data/mysportmanager-shared/lib/config/app_constants.dart"

# Backup original
cp "$CONST_FILE" "$CONST_FILE.bak"

# Replace the localApiUrl (assumes it's on a line by itself)
sed -i "s|static const String localApiUrl = '[^']*'|static const String localApiUrl = 'http://${LAN_IP}/early_access/api'|g" "$CONST_FILE"

echo "✅ Updated localApiUrl to: http://${LAN_IP}/early_access/api"
echo ""

# === Test API Reachability ===
echo "🌐 Testing API reachability from this machine..."
if curl -s "http://${LAN_IP}/early_access/api/dashboard" | grep -q "UNAUTHORIZED\|success"; then
  echo "✅ API is reachable (returned JSON response)"
else
  echo "⚠️  API might not be accessible. Check:"
  echo "   - Your webserver is running and listening on all interfaces"
  echo "   - Firewall allows HTTP traffic on port 80"
  echo "   - IP address is correct"
fi
echo ""

# === Prepare Flutter Project ===
echo "📦 Preparing Flutter project..."
cd /mnt/ssd_data/mysportmanager-shared
flutter pub get > /dev/null 2>&1 && echo "✅ Dependencies fetched" || echo "⚠️  flutter pub get had issues"
echo ""

# === Instructions ===
echo "═════════════════════════════════════════════════════════════"
echo "✅ Setup complete! Next steps:"
echo ""
echo "1. On your phone, enable USB Debugging:"
echo "   Settings → About phone → tap Build number 7x → Developer options → USB Debugging"
echo ""
echo "2. Plug phone via USB to this machine"
echo ""
echo "3. Run the app:"
echo "   export PATH='/mnt/ssd_data/flutter/bin:\$PATH'"
echo "   export JAVA_HOME=/usr/lib/jvm/java-21-openjdk-arm64"
echo "   export ANDROID_HOME=/mnt/ssd_data/android-sdk"
echo "   cd /mnt/ssd_data/mysportmanager-shared"
echo "   flutter run"
echo ""
echo "4. Once app opens, test login with:"
echo "   Username: testuser"
echo "   Password: TestPassword123!"
echo ""
echo "═════════════════════════════════════════════════════════════"
