#!/bin/bash

# cPanel Data Restoration Script
# This script restores the website data to your cPanel hosting

echo "🔄 cPanel Data Restoration Script"
echo "=================================="

# Check if we're in the right directory (should contain the data folder)
if [ ! -d "data" ]; then
    echo "❌ Error: data directory not found in current directory"
    echo "📁 Please run this script from the directory containing the 'data' folder"
    echo "💡 Expected structure:"
    echo "   your-cpanel-directory/"
    echo "   ├── data/"
    echo "   ├── .next/"
    echo "   ├── server.js"
    echo "   └── restore-data-cpanel.sh (this script)"
    exit 1
fi

echo "✅ Found data directory"

# Check what's in the data directory
echo "📊 Checking data directory contents..."
echo "Files in data/:"
ls -la data/

# Count websites
if [ -d "data/website_data" ]; then
    website_count=$(find data/website_data -maxdepth 1 -type d | grep -v "^data/website_data$" | wc -l)
    echo "🌐 Found $website_count websites in website_data/"
    
    echo "📋 Website list:"
    for website in data/website_data/*/; do
        if [ -d "$website" ]; then
            domain=$(basename "$website")
            if [ -f "$website/final_output/index.html" ]; then
                status="✅ Ready"
            else
                status="⚠️ Incomplete"
            fi
            echo "   - $domain: $status"
        fi
    done
else
    echo "⚠️ No website_data directory found"
fi

# Check if credentials exist
if [ -d "data/credentials" ]; then
    echo "🔐 Credentials directory found"
else
    echo "⚠️ No credentials directory found"
fi

# Check user and session data
if [ -f "data/users.json" ]; then
    user_count=$(jq length data/users.json 2>/dev/null || echo "unknown")
    echo "👥 Found users.json with $user_count users"
else
    echo "⚠️ No users.json found"
fi

if [ -f "data/sessions.json" ]; then
    session_count=$(jq length data/sessions.json 2>/dev/null || echo "unknown")
    echo "🔑 Found sessions.json with $session_count sessions"
else
    echo "⚠️ No sessions.json found"
fi

echo ""
echo "🎉 Data restoration check complete!"
echo "📝 Summary:"
echo "   - Website data: $([ -d "data/website_data" ] && echo "✅ Present" || echo "❌ Missing")"
echo "   - Credentials: $([ -d "data/credentials" ] && echo "✅ Present" || echo "❌ Missing")"
echo "   - User data: $([ -f "data/users.json" ] && echo "✅ Present" || echo "❌ Missing")"
echo "   - Session data: $([ -f "data/sessions.json" ] && echo "✅ Present" || echo "❌ Missing")"

echo ""
echo "🚀 Next steps:"
echo "1. Make sure your Node.js application is stopped"
echo "2. Restart your Node.js application: node server.js"
echo "3. Check that the website dashboard loads at your domain"
echo "4. Verify that all websites show up in the dashboard"

echo ""
echo "🔧 Troubleshooting:"
echo "- If websites don't appear, check file permissions: chmod -R 755 data/"
echo "- If you get permission errors, check ownership: chown -R \$USER:\$USER data/"
echo "- Check the debug.log file for any errors"
echo "- Make sure environment variables are set correctly" 