#!/bin/bash

# LuckyChit Flutter Web - Automated Full Deployment
# This script builds AND deploys to production server

# ============================================
# CONFIGURATION - UPDATE THESE VALUES
# ============================================

# Server Configuration
SERVER_USER="luckychit"                     # Your server username
SERVER_HOST="your-server-ip"                # Your server IP or domain
WEB_DIR="/var/www/luckychit"                # Web directory on server
USE_PM2=false                               # Set to true if using PM2 instead of nginx
PM2_APP_NAME="luckychit-web"                # PM2 app name (if using PM2)

# ============================================
# DEPLOYMENT SCRIPT - DO NOT EDIT BELOW
# ============================================

echo "🚀 LuckyChit Flutter Web - Auto Deployment"
echo "==========================================="
echo ""
echo "📋 Configuration:"
echo "   Server: $SERVER_USER@$SERVER_HOST"
echo "   Target: $WEB_DIR"
echo "   Method: $([ "$USE_PM2" = true ] && echo 'PM2' || echo 'nginx')"
echo ""

# Check if we're in the right directory
if [ ! -f "pubspec.yaml" ]; then
    echo "❌ Error: pubspec.yaml not found!"
    echo "Please run this script from the luckychit directory"
    exit 1
fi

# Clean and build
echo "🧹 Step 1/4: Cleaning..."
flutter clean

echo "📦 Step 2/4: Getting dependencies..."
flutter pub get

echo "🔨 Step 3/4: Building for web..."
flutter build web --release

if [ $? -ne 0 ]; then
    echo "❌ Build failed!"
    exit 1
fi

echo "✅ Build successful!"
echo ""

# Deploy to server
echo "📤 Step 4/4: Deploying to production server..."
echo ""

if command -v rsync &> /dev/null; then
    # Use rsync (faster, incremental)
    echo "Using rsync for deployment..."
    rsync -avz --delete build/web/ $SERVER_USER@$SERVER_HOST:$WEB_DIR/
else
    # Fallback to scp
    echo "Using scp for deployment..."
    scp -r build/web/* $SERVER_USER@$SERVER_HOST:$WEB_DIR/
fi

if [ $? -ne 0 ]; then
    echo "❌ Deployment failed!"
    echo "Please check:"
    echo "  - Server credentials are correct"
    echo "  - You have SSH access to the server"
    echo "  - Target directory exists and has write permissions"
    exit 1
fi

echo ""
echo "✅ Files uploaded successfully!"

# Restart service if using PM2
if [ "$USE_PM2" = true ]; then
    echo ""
    echo "🔄 Restarting PM2 service..."
    ssh $SERVER_USER@$SERVER_HOST "pm2 restart $PM2_APP_NAME"
    
    if [ $? -eq 0 ]; then
        echo "✅ PM2 restarted successfully!"
    else
        echo "⚠️  PM2 restart failed - you may need to restart manually"
    fi
fi

echo ""
echo "=========================================="
echo "🎉 Deployment Complete!"
echo ""
echo "🌐 Your app should now be live with the latest changes"
echo ""
echo "📝 Important:"
echo "   - Users may need to clear browser cache (Ctrl+Shift+R)"
echo "   - Test the app in an incognito window first"
echo ""
echo "🔍 Monitor logs:"
if [ "$USE_PM2" = true ]; then
    echo "   ssh $SERVER_USER@$SERVER_HOST 'pm2 logs $PM2_APP_NAME'"
else
    echo "   ssh $SERVER_USER@$SERVER_HOST 'sudo tail -f /var/log/nginx/error.log'"
fi
echo ""

