#!/bin/bash
# Performance Improvements Deployment Script
# Date: 2025-11-12
# Description: Apply Phase 0 performance optimizations

set -e  # Exit on error

# Use docker compose v2 (newer version)
COMPOSE_CMD="docker compose"

echo "================================================"
echo "  Docker Performance Improvements Deployment"
echo "  Date: $(date)"
echo "================================================"
echo ""

# Change to compose files directory
cd /home/icke/compose_files

echo "📊 Checking current resource usage..."
docker stats --no-stream --format "table {{.Name}}\t{{.CPUPerc}}\t{{.MemUsage}}" | grep -E "firefly|zabbix|nextcloud|NAME"
echo ""

# Backup current compose files
echo "💾 Creating backups..."
cp firefly.yml firefly.yml.backup-$(date +%Y%m%d-%H%M%S)
cp zabbix.yml zabbix.yml.backup-$(date +%Y%m%d-%H%M%S)
cp nextcloud.yml nextcloud.yml.backup-$(date +%Y%m%d-%H%M%S)
echo "✅ Backups created"
echo ""

# Apply Firefly improvements
echo "🚀 [1/3] Applying Firefly III improvements..."
echo "  - Adding Redis cache service"
echo "  - Switching from file-based to Redis cache/sessions"
echo "  - Adding database tuning"
$COMPOSE_CMD -f firefly.yml up -d
echo "  ⏳ Waiting for services to start (15 seconds)..."
sleep 15

# Check Firefly status
if docker ps | grep -q "firefly_iii_redis.*Up"; then
    echo "  ✅ Firefly Redis is running"
else
    echo "  ⚠️  WARNING: Firefly Redis may not be running"
fi

if docker ps | grep -q "firefly_iii_core.*Up"; then
    echo "  ✅ Firefly app is running"
else
    echo "  ⚠️  WARNING: Firefly app may not be running"
fi
echo ""

# Apply Zabbix improvements
echo "🚀 [2/3] Applying Zabbix database tuning..."
echo "  - Adding InnoDB buffer pool: 2GB"
echo "  - Adding log file size: 256MB"
echo "  - Setting max connections: 150"
$COMPOSE_CMD -f zabbix.yml restart mysql-zabbix
echo "  ⏳ Waiting for database to stabilize (30 seconds)..."
sleep 30

# Restart Zabbix server to use new database settings
echo "  - Restarting Zabbix server..."
$COMPOSE_CMD -f zabbix.yml restart zabbix-server
echo "  ⏳ Waiting for Zabbix to start (10 seconds)..."
sleep 10

if docker ps | grep -q "mysql-zabbix.*Up"; then
    echo "  ✅ Zabbix database is running"
else
    echo "  ⚠️  WARNING: Zabbix database may not be running"
fi

if docker ps | grep -q "zabbix-server.*Up"; then
    echo "  ✅ Zabbix server is running"
else
    echo "  ⚠️  WARNING: Zabbix server may not be running"
fi
echo ""

# Apply Nextcloud improvements
echo "🚀 [3/3] Applying Nextcloud tmpfs optimization..."
echo "  - Adding /tmp (1GB) and /var/tmp (512MB) tmpfs mounts"
$COMPOSE_CMD -f nextcloud.yml restart app
echo "  ⏳ Waiting for Nextcloud to start (10 seconds)..."
sleep 10

if docker ps | grep -q "compose_files_app_1.*Up"; then
    echo "  ✅ Nextcloud app is running"
else
    echo "  ⚠️  WARNING: Nextcloud app may not be running"
fi
echo ""

# Verification
echo "================================================"
echo "  🔍 VERIFICATION CHECKS"
echo "================================================"
echo ""

echo "1️⃣  Checking Firefly Redis connection..."
if docker exec firefly_iii_redis redis-cli ping 2>/dev/null | grep -q "PONG"; then
    echo "  ✅ Redis responding to ping"
else
    echo "  ❌ Redis not responding"
fi

echo ""
echo "2️⃣  Checking Zabbix database configuration..."
BUFFER_SIZE=$(docker exec mysql-zabbix mysql -uroot -p'eccmts42*' -sN -e "SHOW VARIABLES LIKE 'innodb_buffer_pool_size';" 2>/dev/null | awk '{print $2}')
if [ "$BUFFER_SIZE" = "2147483648" ]; then
    echo "  ✅ InnoDB buffer pool: 2GB (correct)"
else
    echo "  ⚠️  InnoDB buffer pool: $BUFFER_SIZE (expected: 2147483648)"
fi

MAX_CONN=$(docker exec mysql-zabbix mysql -uroot -p'eccmts42*' -sN -e "SHOW VARIABLES LIKE 'max_connections';" 2>/dev/null | awk '{print $2}')
if [ "$MAX_CONN" = "150" ]; then
    echo "  ✅ Max connections: 150 (correct)"
else
    echo "  ⚠️  Max connections: $MAX_CONN (expected: 150)"
fi

echo ""
echo "3️⃣  Checking Nextcloud tmpfs mounts..."
if docker exec compose_files_app_1 df -h 2>/dev/null | grep -q "tmpfs.*\/tmp"; then
    echo "  ✅ /tmp tmpfs mounted"
else
    echo "  ⚠️  /tmp tmpfs not found"
fi

if docker exec compose_files_app_1 df -h 2>/dev/null | grep -q "tmpfs.*\/var\/tmp"; then
    echo "  ✅ /var/tmp tmpfs mounted"
else
    echo "  ⚠️  /var/tmp tmpfs not found"
fi

echo ""
echo "================================================"
echo "  📊 NEW RESOURCE USAGE"
echo "================================================"
docker stats --no-stream --format "table {{.Name}}\t{{.CPUPerc}}\t{{.MemUsage}}" | grep -E "firefly|zabbix|nextcloud|redis|NAME"
echo ""

echo "================================================"
echo "  ✅ DEPLOYMENT COMPLETE"
echo "================================================"
echo ""
echo "Next steps:"
echo "  1. Monitor logs for errors:"
echo "     docker compose -f firefly.yml logs -f firefly"
echo "     docker compose -f zabbix.yml logs -f mysql-zabbix"
echo "     docker compose -f nextcloud.yml logs -f app"
echo ""
echo "  2. Test performance:"
echo "     - Firefly: Load dashboard and transactions"
echo "     - Zabbix: Check monitoring graphs"
echo "     - Nextcloud: Upload file and generate preview"
echo ""
echo "  3. Check detailed deployment notes:"
echo "     cat PERFORMANCE_IMPROVEMENTS_2025-11-12.md"
echo ""
echo "Backup files created with timestamp for rollback if needed."
echo "================================================"
