#!/bin/bash
# Website Consolidation and Full Deployment Script
# Deploys from /media/pts/Website/SkyArtShop/website to /var/www/skyartshop

set -e  # Exit on error

REPO_DIR="/media/pts/Website/SkyArtShop"
DEV_DIR="$REPO_DIR/website"
DEPLOY_DIR="/var/www/skyartshop"
BACKUP_DIR="/var/www/backups/skyartshop-$(date +%Y%m%d-%H%M%S)"

echo "========================================="
echo "  Sky Art Shop - Full Website Deployment"
echo "========================================="
echo ""
echo "📁 Source (Dev):  $DEV_DIR"
echo "🌐 Destination:   $DEPLOY_DIR"
echo "💾 Backup:        $BACKUP_DIR"
echo ""

# Create backup
echo "📦 Creating backup of current production..."
sudo mkdir -p "$BACKUP_DIR"
sudo cp -r "$DEPLOY_DIR" "$BACKUP_DIR/" 2>/dev/null || true
echo "✅ Backup created"
echo ""

# Deploy files
echo "🚀 Deploying website files..."

# Deploy public frontend
echo "  → Public pages..."
sudo rm -rf "$DEPLOY_DIR/public"
sudo mkdir -p "$DEPLOY_DIR/public"
sudo cp -r "$DEV_DIR/public/"* "$DEPLOY_DIR/public/" 2>/dev/null || true
echo "    ✓ Public pages deployed"

# Deploy admin panel
echo "  → Admin panel..."
sudo rm -rf "$DEPLOY_DIR/admin"
sudo mkdir -p "$DEPLOY_DIR/admin"
sudo cp -r "$DEV_DIR/admin/"* "$DEPLOY_DIR/admin/" 2>/dev/null || true
echo "    ✓ Admin panel deployed"

# Deploy assets
echo "  → Assets..."
sudo rm -rf "$DEPLOY_DIR/assets"
sudo mkdir -p "$DEPLOY_DIR/assets"
sudo cp -r "$DEV_DIR/assets/"* "$DEPLOY_DIR/assets/" 2>/dev/null || true
echo "    ✓ Assets deployed"

# Create uploads directory if it doesn't exist
sudo mkdir -p "$DEPLOY_DIR/uploads"
sudo mkdir -p "$DEPLOY_DIR/uploads/products"
sudo mkdir -p "$DEPLOY_DIR/uploads/portfolio"
sudo mkdir -p "$DEPLOY_DIR/uploads/blog"

echo ""
echo "🔒 Setting permissions..."
sudo chown -R pts:pts "$DEPLOY_DIR"
sudo chmod -R 755 "$DEPLOY_DIR"
sudo chmod -R 775 "$DEPLOY_DIR/uploads"

echo ""
echo "🔄 Reloading nginx..."
sudo nginx -t && sudo systemctl reload nginx

echo ""
echo "========================================="
echo "  ✅ Deployment Complete!"
echo "========================================="
echo ""
echo "🌐 Your website is accessible at:"
echo "   • https://skyarts.ddns.net"
echo "   • http://localhost"
echo ""
echo "🔐 Admin Panel:"
echo "   • https://skyarts.ddns.net/admin/login.html"
echo "   • http://localhost:5000/admin/login.html"
echo ""
echo "⚙️  Backend API:"
echo "   • Port 5000 (PM2: skyartshop)"
echo "   • Status: pm2 status"
echo ""
echo "💡 Note: localhost and skyarts.ddns.net both serve"
echo "   the same website - they're just different URLs"
echo "   pointing to the same content."
echo ""

# Sync assets (but don't delete to preserve any user uploads)
rsync -av website/assets/ $DEPLOY_DIR/assets/ 2>/dev/null || cp -r website/assets/* $DEPLOY_DIR/assets/

# Fix permissions
chown -R pts:pts $DEPLOY_DIR/public $DEPLOY_DIR/admin $DEPLOY_DIR/assets 2>/dev/null || true
chmod -R 644 $DEPLOY_DIR/public/*.html $DEPLOY_DIR/admin/*.html 2>/dev/null || true

echo ""
echo "========================================="
echo "✅ Deployment Complete!"
echo "========================================="
echo ""
echo "🌐 Website: https://skyarts.ddns.net"
echo "🔐 Admin:   https://skyarts.ddns.net/admin"
echo ""
echo "📝 Next steps:"
echo "   1. Edit files in: $REPO_DIR/website/"
echo "   2. Run this script to deploy"
echo "   3. Commit changes: git add . && git commit -m 'Update website'"
echo ""
