#!/bin/bash
#
# Local Deploy Script - CarneiroTech to OCI
# Usage: ./deploy-to-oci.sh
#

set -e

SERVER="ubuntu@129.146.116.218"
APP_DIR="/home/ubuntu/apps/carneirotech"
LOCAL_DIR="$(cd "$(dirname "$0")" && pwd)"

echo "🚀 CarneiroTech - Deploy to OCI"
echo "================================"
echo ""

# Create tarball excluding unnecessary files
echo "📦 Creating deployment package..."
cd "$LOCAL_DIR"
tar --exclude='bin' \
    --exclude='obj' \
    --exclude='.vs' \
    --exclude='*.user' \
    --exclude='.git' \
    --exclude='deploy-to-oci.sh' \
    -czf /tmp/carneirotech-deploy.tar.gz .

echo "✅ Package created: $(du -h /tmp/carneirotech-deploy.tar.gz | cut -f1)"
echo ""

# Transfer to server
echo "📤 Transferring to server..."
scp /tmp/carneirotech-deploy.tar.gz "$SERVER:$APP_DIR/"
rm /tmp/carneirotech-deploy.tar.gz

# Extract and deploy on server
echo "📥 Extracting on server..."
ssh "$SERVER" << 'ENDSSH'
cd /home/ubuntu/apps/carneirotech
tar -xzf carneirotech-deploy.tar.gz
rm carneirotech-deploy.tar.gz
echo "✅ Files extracted"
echo ""

# Run deploy script
echo "🚀 Running deployment..."
./deploy.sh
ENDSSH

echo ""
echo "✨ Deployment completed!"
echo "🌐 Check your site at: https://carneirotech.com"
