#!/bin/bash
# Deploy mainroom to server (amar + soleprint)
#
# Two deployment modes:
#   1. Docker (default): Full mainroom structure + source code
#   2. Bare metal (--bare-metal): Only soleprint source to systemd services
#
# Usage:
#   ./deploy.sh                  # Deploy Docker setup (default)
#   ./deploy.sh --bare-metal     # Deploy bare metal soleprint only
#   ./deploy.sh --dry-run        # Preview what would be synced
#   ./deploy.sh --bare-metal --dry-run  # Preview bare metal sync

set -e

# Load configuration
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
source "$SCRIPT_DIR/.env.sync" 2>/dev/null || true

SERVER="${DEPLOY_SERVER:-mariano@mcrn.ar}"
REMOTE_PATH="${DEPLOY_REMOTE_PATH:-~/mainroom}"
BARE_METAL_PATH="${DEPLOY_BARE_METAL_PATH:-~/soleprint}"

# Source code paths (defaults if not in .env.sync)
LOCAL_AMAR_BACKEND="${LOCAL_AMAR_BACKEND:-$HOME/wdir/ama/amar_django_back}"
LOCAL_AMAR_FRONTEND="${LOCAL_AMAR_FRONTEND:-$HOME/wdir/ama/amar_frontend}"
LOCAL_SOLEPRINT="${LOCAL_SOLEPRINT:-$HOME/wdir/spr/gen}"

DRY_RUN=""
BARE_METAL=""

for arg in "$@"; do
    case $arg in
        --dry-run) DRY_RUN="--dry-run" ;;
        --bare-metal) BARE_METAL="true" ;;
    esac
done

cd "$SCRIPT_DIR/.."  # Go to root (parent of ctrl/)

# Common rsync options (using single .exclude file for everything)
RSYNC_CMD="rsync -avz --mkpath --delete --progress --exclude-from=ctrl/.exclude"

# =============================================================================
# BARE METAL DEPLOYMENT
# =============================================================================
if [ -n "$BARE_METAL" ]; then
    echo "=== Deploying to Bare Metal (Systemd Services) ==="
    echo ""

    # Only sync soleprint source
    if [ -d "$LOCAL_SOLEPRINT" ] && [ -f "$LOCAL_SOLEPRINT/main.py" ]; then
        echo "Syncing soleprint source to bare metal..."
        $RSYNC_CMD $DRY_RUN \
            "$LOCAL_SOLEPRINT/" \
            "$SERVER:$BARE_METAL_PATH/"
        echo "  ✓ Soleprint synced to $BARE_METAL_PATH"
    else
        echo "⚠ Soleprint not found at: $LOCAL_SOLEPRINT"
        exit 1
    fi
    echo ""

    if [ -n "$DRY_RUN" ]; then
        echo "=== Dry run complete ==="
        exit 0
    fi

    echo "=== Bare Metal Sync Complete ==="
    echo ""
    echo "Next steps on server (as mariano user):"
    echo "  Restart systemd services:"
    echo "    sudo systemctl restart soleprint artery album ward"
    echo ""
    echo "  Check status:"
    echo "    sudo systemctl status soleprint artery album ward"
    echo ""
    exit 0
fi

# =============================================================================
# DOCKER DEPLOYMENT (DEFAULT)
# =============================================================================
echo "=== Deploying to Docker ==="
echo ""

# 1. Sync mainroom structure (excluding src directories - they're synced separately)
echo "1. Syncing mainroom structure..."
$RSYNC_CMD $DRY_RUN \
    --exclude='*/src/' \
    ./ \
    "$SERVER:$REMOTE_PATH/"
echo "  [OK] Core room structure synced"
echo ""

# 2. Sync amar backend source
if [ -d "$LOCAL_AMAR_BACKEND" ]; then
    echo "2. Syncing amar backend source..."
    $RSYNC_CMD $DRY_RUN \
        "$LOCAL_AMAR_BACKEND/" \
        "$SERVER:$REMOTE_PATH/amar/src/back/"
    echo "  [OK] Backend synced"
else
    echo "2. [WARN] Backend source not found at: $LOCAL_AMAR_BACKEND"
fi
echo ""

# 3. Sync amar frontend source
if [ -d "$LOCAL_AMAR_FRONTEND" ]; then
    echo "3. Syncing amar frontend source..."
    $RSYNC_CMD $DRY_RUN \
        "$LOCAL_AMAR_FRONTEND/" \
        "$SERVER:$REMOTE_PATH/amar/src/front/"
    echo "  [OK] Frontend synced"
else
    echo "3. [WARN] Frontend source not found at: $LOCAL_AMAR_FRONTEND"
fi
echo ""

# 4. Sync soleprint source
if [ -d "$LOCAL_SOLEPRINT" ] && [ -f "$LOCAL_SOLEPRINT/main.py" ]; then
    echo "4. Syncing soleprint source..."
    $RSYNC_CMD $DRY_RUN \
        "$LOCAL_SOLEPRINT/" \
        "$SERVER:$REMOTE_PATH/soleprint/src/"
    echo "  [OK] Soleprint synced"
else
    echo "4. [INFO] Soleprint not found at: $LOCAL_SOLEPRINT"
fi
echo ""

# 5. Sync tests to ward (silent fail if not available)
if [ -z "$DRY_RUN" ]; then
    echo "5. Syncing tests to ward..."
    if SILENT_FAIL=true "$SCRIPT_DIR/sync-tests.sh" >/dev/null 2>&1; then
        echo "  [OK] Tests synced"
    else
        echo "  [SKIP] Tests sync not configured or not available"
    fi
    echo ""
fi

if [ -n "$DRY_RUN" ]; then
    echo "=== Dry run complete ==="
    exit 0
fi

echo "=== Docker Sync Complete ==="
echo ""
echo "Next steps on server (as mariano user):"
echo "  1. Setup (first time only):"
echo "       ssh $SERVER 'cd $REMOTE_PATH/ctrl/server && ./setup.sh'"
echo ""
echo "  2. Setup test symlinks (optional, enables test sharing):"
echo "       ssh $SERVER 'cd $REMOTE_PATH/ctrl/server && ./setup-symlinks.sh'"
echo "       Or sync tests without symlinks: ./ctrl/sync-tests.sh"
echo ""
echo "  3. Build and start:"
echo "       ssh $SERVER 'cd $REMOTE_PATH/ctrl && ./build.sh && ./start.sh -d'"
echo ""
echo "Note: Bare metal services remain running as fallback (*.bare.mcrn.ar)"
