#!/bin/bash

# Nextcloud Upgrade Script
# Upgrades from 27.1.3 to 31.0.8 with comprehensive compatibility checks
# Created: $(date)

set -e

# Configuration
CURRENT_VERSION="27.1.3"
TARGET_VERSION="31.0.8"
BACKUP_DIR="/export/nextcloud_backups"
SERVICE_NAME="nextcloud_nextcloud"
CONTAINER_NAME="nextcloud"

echo "=== Nextcloud Upgrade Script (27.1.3 → 31.0.8) ==="
echo "Current Version: $CURRENT_VERSION"
echo "Target Version: $TARGET_VERSION"
echo ""

# Pre-upgrade compatibility checks
echo "=== PRE-UPGRADE COMPATIBILITY CHECKS ==="

# Check current configuration
echo "1. Checking current configuration..."
CURRENT_CONFIG=$(ssh root@omv800 "docker exec -u 33 \$(docker ps -q -f name=nextcloud) php /var/www/html/occ config:list system")

# Check database type
DB_TYPE=$(echo "$CURRENT_CONFIG" | grep '"dbtype"' | cut -d'"' -f4)
echo "   Database Type: $DB_TYPE"

# Check PHP version
PHP_VERSION=$(ssh root@omv800 "docker exec -u 33 \$(docker ps -q -f name=nextcloud) php -r 'echo PHP_VERSION;'")
echo "   PHP Version: $PHP_VERSION"

# Check enabled apps
echo "2. Checking enabled apps..."
ENABLED_APPS=$(ssh root@omv800 "docker exec -u 33 \$(docker ps -q -f name=nextcloud) php /var/www/html/occ app:list" | grep -A 50 "Enabled:" | grep "  - " | cut -d' ' -f3)

echo "   Enabled Apps:"
echo "$ENABLED_APPS" | while read app; do
    if [ -n "$app" ]; then
        echo "     - $app"
    fi
done

# Compatibility warnings
echo ""
echo "=== COMPATIBILITY WARNINGS ==="

# Database compatibility
if [ "$DB_TYPE" = "sqlite3" ]; then
    echo "⚠️  WARNING: Using SQLite3 database on MergerFS"
    echo "   - This can cause database locking issues during upgrade"
    echo "   - Recommend migrating database to local drive first"
    echo "   - Will perform database migration as part of upgrade process"
fi

# PHP version check
PHP_MAJOR=$(echo $PHP_VERSION | cut -d'.' -f1)
PHP_MINOR=$(echo $PHP_VERSION | cut -d'.' -f2)
if [ "$PHP_MAJOR" -lt 8 ] || ([ "$PHP_MAJOR" -eq 8 ] && [ "$PHP_MINOR" -lt 2 ]); then
    echo "❌ ERROR: PHP version $PHP_VERSION is not compatible with Nextcloud 31"
    echo "   - Nextcloud 31 requires PHP 8.2 or higher"
    echo "   - Current PHP version: $PHP_VERSION"
    exit 1
else
    echo "✅ PHP version $PHP_VERSION is compatible with Nextcloud 31"
fi

# App compatibility check
echo ""
echo "3. Checking app compatibility..."
INCOMPATIBLE_APPS=""
for app in $ENABLED_APPS; do
    # Check for known incompatible apps
    case $app in
        "cookbook"|"maps"|"tables"|"tasks")
            INCOMPATIBLE_APPS="$INCOMPATIBLE_APPS $app"
            echo "   ⚠️  $app may have compatibility issues with Nextcloud 31"
            ;;
    esac
done

if [ -n "$INCOMPATIBLE_APPS" ]; then
    echo ""
    echo "⚠️  WARNING: Some apps may be incompatible:"
    echo "$INCOMPATIBLE_APPS"
    echo "   - These apps may need to be disabled or updated"
    echo "   - Check app compatibility before proceeding"
fi

echo ""
echo "=== UPGRADE PROCEEDURE ==="
read -p "Do you want to proceed with the upgrade? (y/N): " -n 1 -r
echo
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
    echo "Upgrade cancelled."
    exit 0
fi

# Step 1: Database Migration (if using SQLite3 on MergerFS)
if [ "$DB_TYPE" = "sqlite3" ]; then
    echo ""
    echo "=== STEP 1: DATABASE MIGRATION ==="
    echo "Migrating database from MergerFS to local drive to prevent locking issues..."
    
    # Configuration for database migration
    NEXTCLOUD_DATA_DIR="/export/nextcloud/data"
    LOCAL_DB_DIR="/srv/dev-disk-by-uuid-0f772f0b-917d-4337-a3c5-5cc5d3badac9/nextcloud-db"
    
    echo "Available space on target drive:"
    ssh root@omv800 "df -h /srv/dev-disk-by-uuid-0f772f0b-917d-4337-a3c5-5cc5d3badac9"
    
    # Create local database directory
    echo "Creating local database directory..."
    ssh root@omv800 "mkdir -p $LOCAL_DB_DIR"
    ssh root@omv800 "chown -R 33:33 $LOCAL_DB_DIR"
    
    # Stop Nextcloud service
    echo "Stopping Nextcloud service..."
    ssh root@omv800 "docker service scale $SERVICE_NAME=0"
    sleep 30
    
    # Copy database to local storage
    echo "Copying database to local drive..."
    ssh root@omv800 "cp -r $NEXTCLOUD_DATA_DIR/* $LOCAL_DB_DIR/"
    ssh root@omv800 "chown -R 33:33 $LOCAL_DB_DIR"
    
    # Update docker-compose configuration
    echo "Updating docker-compose configuration..."
    cd /home/jonathan/Coding/HomeAudit
    
    # Create backup of current config
    cp stacks/apps/nextcloud.yml stacks/apps/nextcloud.yml.db_migration_backup.$(date +%Y%m%d_%H%M%S)
    
    # Update the volume mapping to use local database storage
    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
    
    # Copy updated config to OMV800
    echo "Copying updated configuration to OMV800..."
    scp stacks/apps/nextcloud.yml root@omv800:/tmp/nextcloud.yml
    
    # Deploy updated service
    echo "Deploying service with local database..."
    ssh root@omv800 "docker stack deploy -c /tmp/nextcloud.yml nextcloud"
    sleep 60
    
    # Verify database migration
    if ssh root@omv800 "docker service ls | grep -q $SERVICE_NAME"; then
        echo "✅ Database migration completed successfully"
        sleep 30
        CONTAINER_ID=$(ssh root@omv800 "docker ps -q -f name=nextcloud")
        if [ -n "$CONTAINER_ID" ]; then
            ssh root@omv800 "docker exec -u 33 $CONTAINER_ID php /var/www/html/occ status"
        fi
    else
        echo "❌ Database migration failed"
        exit 1
    fi
fi

# Step 2: Create backup
echo ""
echo "=== STEP 2: CREATING BACKUP ==="
echo "Creating backup..."
mkdir -p "$BACKUP_DIR"
BACKUP_FILE="$BACKUP_DIR/nextcloud_backup_$(date +%Y%m%d_%H%M%S).tar.gz"

echo "Creating backup: $BACKUP_FILE"
ssh root@omv800 "cd /export && tar -czf $BACKUP_FILE nextcloud/"
echo "✅ Backup created: $BACKUP_FILE"

# Step 3: Stop Nextcloud service
echo ""
echo "=== STEP 3: STOPPING SERVICE ==="
echo "Stopping Nextcloud service..."
ssh root@omv800 "docker service scale $SERVICE_NAME=0"
echo "Waiting for service to stop..."
sleep 30

# Step 4: Update docker-compose file
echo ""
echo "=== STEP 4: UPDATING CONFIGURATION ==="
echo "Updating docker-compose configuration..."
cd /home/jonathan/Coding/HomeAudit

# Create backup of current config
cp stacks/apps/nextcloud.yml stacks/apps/nextcloud.yml.backup.$(date +%Y%m%d_%H%M%S)

# Update the image version
sed -i "s|image: nextcloud:$CURRENT_VERSION|image: nextcloud:$TARGET_VERSION|g" stacks/apps/nextcloud.yml

echo "✅ Configuration updated to version $TARGET_VERSION"

# Copy updated config to OMV800
echo "Copying updated configuration to OMV800..."
scp stacks/apps/nextcloud.yml root@omv800:/tmp/nextcloud.yml

# Step 5: Deploy updated service
echo ""
echo "=== STEP 5: DEPLOYING UPDATED SERVICE ==="
echo "Deploying updated Nextcloud service..."
ssh root@omv800 "docker stack deploy -c /tmp/nextcloud.yml nextcloud"

echo "Waiting for service to start..."
sleep 60

# Step 6: Check service status
echo ""
echo "=== STEP 6: CHECKING SERVICE STATUS ==="
echo "Checking service status..."
if ssh root@omv800 "docker service ls | grep -q $SERVICE_NAME"; then
    echo "✅ Service is running"
else
    echo "❌ Service failed to start"
    echo "Rolling back..."
    ssh root@omv800 "docker service scale $SERVICE_NAME=0"
    cp stacks/apps/nextcloud.yml.backup.* stacks/apps/nextcloud.yml
    scp stacks/apps/nextcloud.yml root@omv800:/tmp/nextcloud.yml
    ssh root@omv800 "docker stack deploy -c /tmp/nextcloud.yml nextcloud"
    exit 1
fi

# Step 7: Wait for container to be ready
echo ""
echo "=== STEP 7: WAITING FOR CONTAINER ==="
echo "Waiting for container to be ready..."
sleep 30

# Step 8: Run Nextcloud upgrade
echo ""
echo "=== STEP 8: RUNNING UPGRADE ==="
echo "Running Nextcloud upgrade..."
CONTAINER_ID=$(ssh root@omv800 "docker ps -q -f name=nextcloud")

if [ -n "$CONTAINER_ID" ]; then
    echo "Running upgrade command..."
    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"
    
    echo "✅ Upgrade completed successfully"
else
    echo "❌ Container not found"
    exit 1
fi

# Step 9: Verify version and check for issues
echo ""
echo "=== STEP 9: VERIFICATION ==="
echo "Verifying new version and checking for issues..."
ssh root@omv800 "docker exec -u 33 $CONTAINER_ID php /var/www/html/occ status"

# Check for broken apps
echo ""
echo "Checking for broken apps..."
ssh root@omv800 "docker exec -u 33 $CONTAINER_ID php /var/www/html/occ app:list" | grep -A 50 "Disabled:" | grep -E "(incompatible|broken)" || echo "No broken apps detected"

# Check system requirements
echo ""
echo "Checking system requirements..."
ssh root@omv800 "docker exec -u 33 $CONTAINER_ID php /var/www/html/occ check"

echo ""
echo "=== UPGRADE SUMMARY ==="
echo "✅ Backup created: $BACKUP_FILE"
echo "✅ Database migrated to local drive (if SQLite3)"
echo "✅ Service updated to version $TARGET_VERSION"
echo "✅ Database upgraded and optimized"
echo "✅ Files scanned and indexed"
echo ""
echo "Nextcloud is now running version $TARGET_VERSION"
echo "Access at: https://nextcloud.pressmess.duckdns.org"
echo ""
echo "=== POST-UPGRADE RECOMMENDATIONS ==="
echo "1. Test all enabled apps for functionality"
echo "2. Check file uploads and downloads"
echo "3. Verify user authentication and permissions"
echo "4. Monitor system performance"
echo "5. Database is now on local drive for better performance"
echo ""
echo "If you encounter issues, restore from backup:"
echo "tar -xzf $BACKUP_FILE -C /export/"
