#!/bin/bash
###############################################################################
# Fix Theme Issues and Deploy Frontend
###############################################################################

SERVER_IP="192.168.8.148"
cd /home/luckychit/apps/chitfund/luckychit

echo "🔧 Fixing theme issues..."
# Fix CardTheme to CardThemeData
sed -i 's/cardTheme: CardTheme(/cardTheme: CardThemeData(/g' lib/core/themes/app_theme.dart

# Fix DialogTheme to DialogThemeData
sed -i 's/dialogTheme: DialogTheme(/dialogTheme: DialogThemeData(/g' lib/core/themes/app_theme.dart

echo "✅ Theme fixes applied"

echo "🔧 Updating API URL..."
sed -i "s|http://localhost:3000/api|http://${SERVER_IP}:3000/api|g" lib/core/services/api_service.dart

echo "📦 Getting dependencies..."
flutter pub get

echo "🏗️  Building web app..."
flutter build web --release

echo "🚀 Deploying..."
mkdir -p /var/www/luckychit
cp -r build/web/* /var/www/luckychit/
chown -R www-data:www-data /var/www/luckychit

echo "🔄 Restoring original files..."
git checkout lib/core/services/api_service.dart 2>/dev/null || sed -i "s|http://${SERVER_IP}:3000/api|http://localhost:3000/api|g" lib/core/services/api_service.dart

echo "✅ Done! Visit: http://${SERVER_IP}"

