#!/bin/bash

# Nextcloud Step-by-Step Upgrade Script
# Upgrades from 27.1.3 to 31.0.8 one major version at a time
# Created: $(date)

set -e

echo "=== Nextcloud Step-by-Step Upgrade Script ==="
echo "Upgrading from 27.1.3 to 31.0.8 (one major version at a time)"
echo ""

# Function to get container ID
get_container_id() {
    ssh root@omv800 "docker ps -q -f name=nextcloud" | head -1
}

# Function to wait for container
wait_for_container() {
    echo "Waiting for Nextcloud container to be ready..."
    for i in {1..30}; do
        CONTAINER_ID=$(get_container_id)
        if [ -n "$CONTAINER_ID" ]; then
            echo "✅ Container found: $CONTAINER_ID"
            return 0
        fi
        echo "Waiting... ($i/30)"
        sleep 10
    done
    echo "❌ Container not found after 5 minutes"
    return 1
}

# Function to wait for container to be healthy
wait_for_healthy() {
    echo "Waiting for container to be healthy..."
    for i in {1..60}; do
        HEALTH=$(ssh root@omv800 "docker inspect $1 --format='{{.State.Health.Status}}' 2>/dev/null || echo 'starting'")
        if [ "$HEALTH" = "healthy" ]; then
            echo "✅ Container is healthy"
            return 0
        fi
        echo "Health status: $HEALTH ($i/60)"
        sleep 10
    done
    echo "❌ Container not healthy after 10 minutes"
    return 1
}

# Function to upgrade to specific version
upgrade_to_version() {
    local TARGET_VERSION=$1
    local VERSION_NAME=$2
    
    echo ""
    echo "=== UPGRADING TO NEXTCLOUD $VERSION_NAME ==="
    echo "Target version: $TARGET_VERSION"
    
    # Stop service
    echo "Stopping Nextcloud service..."
    ssh root@omv800 "docker service scale nextcloud_nextcloud=0"
    sleep 30
    
    # Update configuration
    echo "Updating to Nextcloud $VERSION_NAME..."
    cd /home/jonathan/Coding/HomeAudit
    sed -i "s|image: nextcloud:[0-9.]*|image: nextcloud:$TARGET_VERSION|g" stacks/apps/nextcloud.yml
    
    # Deploy new version
    echo "Deploying Nextcloud $VERSION_NAME..."
    scp stacks/apps/nextcloud.yml root@omv800:/tmp/nextcloud.yml
    ssh root@omv800 "docker stack deploy -c /tmp/nextcloud.yml nextcloud"
    
    # Wait for container
    wait_for_container || exit 1
    CONTAINER_ID=$(get_container_id)
    
    # Wait for container to be healthy
    wait_for_healthy $CONTAINER_ID || {
        echo "❌ Container not healthy, checking logs..."
        ssh root@omv800 "docker service logs nextcloud_nextcloud --tail 20"
        exit 1
    }
    
    # Run upgrade
    echo "Running Nextcloud upgrade..."
    ssh root@omv800 "docker exec -u 33 $CONTAINER_ID php /var/www/html/occ upgrade"
    
    echo "Running database maintenance..."
    ssh root@omv800 "docker exec -u 33 $CONTAINER_ID php /var/www/html/occ db:add-missing-indices"
    ssh root@omv800 "docker exec -u 33 $CONTAINER_ID php /var/www/html/occ db:convert-filecache-bigint"
    
    echo "Scanning files..."
    ssh root@omv800 "docker exec -u 33 $CONTAINER_ID php /var/www/html/occ files:scan --all"
    ssh root@omv800 "docker exec -u 33 $CONTAINER_ID php /var/www/html/occ files:scan-app-data"
    
    # Verify version
    echo "Verifying version..."
    ssh root@omv800 "docker exec -u 33 $CONTAINER_ID php /var/www/html/occ status"
    
    echo "✅ Upgrade to $VERSION_NAME completed successfully"
}

# Step 1: Check current status
echo "=== STEP 1: CHECKING CURRENT STATUS ==="
CONTAINER_ID=$(get_container_id)
if [ -z "$CONTAINER_ID" ]; then
    echo "Starting Nextcloud service..."
    ssh root@omv800 "docker service scale nextcloud_nextcloud=1"
    wait_for_container || exit 1
    CONTAINER_ID=$(get_container_id)
fi

echo "Current container: $CONTAINER_ID"

# Check current version
echo "Checking current version..."
ssh root@omv800 "docker exec -u 33 $CONTAINER_ID php /var/www/html/occ status"

# Check database type
echo "Checking database configuration..."
DB_TYPE=$(ssh root@omv800 "docker exec -u 33 $CONTAINER_ID php /var/www/html/occ config:list system" | grep '"dbtype"' | cut -d'"' -f4)
echo "Database type: $DB_TYPE"

# Step 2: Database Migration (if SQLite3)
if [ "$DB_TYPE" = "sqlite3" ]; then
    echo ""
    echo "=== STEP 2: DATABASE MIGRATION ==="
    echo "Migrating database from MergerFS to local drive..."
    
    # Stop service
    echo "Stopping Nextcloud service..."
    ssh root@omv800 "docker service scale nextcloud_nextcloud=0"
    sleep 30
    
    # Create local database directory
    LOCAL_DB_DIR="/srv/dev-disk-by-uuid-0f772f0b-917d-4337-a3c5-5cc5d3badac9/nextcloud-db"
    echo "Creating local database directory: $LOCAL_DB_DIR"
    ssh root@omv800 "mkdir -p $LOCAL_DB_DIR"
    ssh root@omv800 "chown -R 33:33 $LOCAL_DB_DIR"
    
    # Copy database
    echo "Copying database to local drive..."
    ssh root@omv800 "cp -r /export/nextcloud/data/* $LOCAL_DB_DIR/"
    ssh root@omv800 "chown -R 33:33 $LOCAL_DB_DIR"
    
    # Update configuration
    echo "Updating Docker configuration..."
    cd /home/jonathan/Coding/HomeAudit
    cp stacks/apps/nextcloud.yml stacks/apps/nextcloud.yml.backup.$(date +%Y%m%d_%H%M%S)
    
    # Remove any existing data volume mounts and add the local one
    sed -i '/\/var\/www\/html\/data/d' stacks/apps/nextcloud.yml
    sed -i 's|- /export/nextcloud:/var/www/html|- /export/nextcloud:/var/www/html\n      - /srv/dev-disk-by-uuid-0f772f0b-917d-4337-a3c5-5cc5d3badac9/nextcloud-db:/var/www/html/data|g' stacks/apps/nextcloud.yml
    
    # Deploy with local database
    echo "Deploying with local database..."
    scp stacks/apps/nextcloud.yml root@omv800:/tmp/nextcloud.yml
    ssh root@omv800 "docker stack deploy -c /tmp/nextcloud.yml nextcloud"
    
    # Wait for container
    wait_for_container || exit 1
    CONTAINER_ID=$(get_container_id)
    
    echo "✅ Database migration completed"
fi

# Step 3: Create backup
echo ""
echo "=== STEP 3: CREATING BACKUP ==="
BACKUP_FILE="/export/nextcloud_backups/nextcloud_backup_$(date +%Y%m%d_%H%M%S).tar.gz"
echo "Creating backup: $BACKUP_FILE"
ssh root@omv800 "mkdir -p /export/nextcloud_backups"
ssh root@omv800 "cd /export && tar -czf $BACKUP_FILE nextcloud/"
echo "✅ Backup created"

# Step 4: Step-by-step upgrades
echo ""
echo "=== STEP 4: STEP-BY-STEP UPGRADES ==="

# Upgrade to version 28
upgrade_to_version "28.0.12" "28"

# Upgrade to version 29
upgrade_to_version "29.0.10" "29"

# Upgrade to version 30
upgrade_to_version "30.0.10" "30"

# Upgrade to version 31
upgrade_to_version "31.0.8" "31"

# Final verification
echo ""
echo "=== FINAL VERIFICATION ==="
echo "Checking final version..."
ssh root@omv800 "docker exec -u 33 $CONTAINER_ID php /var/www/html/occ status"

echo "Checking for issues..."
ssh root@omv800 "docker exec -u 33 $CONTAINER_ID php /var/www/html/occ check"

echo ""
echo "=== UPGRADE COMPLETED SUCCESSFULLY ==="
echo "✅ Nextcloud upgraded from 27.1.3 to 31.0.8"
echo "✅ Database migrated to local drive (if SQLite3)"
echo "✅ Backup created: $BACKUP_FILE"
echo "✅ All intermediate versions completed successfully"
echo ""
echo "Access at: https://nextcloud.pressmess.duckdns.org"
echo ""
echo "If you encounter issues, restore from backup:"
echo "tar -xzf $BACKUP_FILE -C /export/"
