#!/bin/bash
# Production deployment script for rollover interest change
# Changes interest from percentage to actual KES amount

echo "🚀 Deploying Rollover Interest Amount Change to Production"
echo "============================================================"

# Step 1: Backup current database
echo ""
echo "📦 Step 1: Creating database backup..."
BACKUP_FILE="backup_before_interest_change_$(date +%Y%m%d_%H%M%S).sql"
mysqldump -u xygbfpsg_graz -p'j.ez-xy6##y.rllB' xygbfpsg_loans loans_rolloverrequest > "$BACKUP_FILE"

if [ $? -eq 0 ]; then
    echo "✅ Database backup created: $BACKUP_FILE"
else
    echo "❌ Database backup failed! Aborting deployment."
    exit 1
fi

# Step 2: Check for pending rollovers
echo ""
echo "📊 Step 2: Checking for pending rollover requests..."
PENDING_COUNT=$(mysql -u xygbfpsg_graz -p'j.ez-xy6##y.rllB' -D xygbfpsg_loans -se "SELECT COUNT(*) FROM loans_rolloverrequest WHERE status='pending' AND requested_interest_rate IS NOT NULL;")

echo "Found $PENDING_COUNT pending rollover requests with custom interest"

if [ "$PENDING_COUNT" -gt 0 ]; then
    echo ""
    echo "⚠️  WARNING: You have $PENDING_COUNT pending rollover requests!"
    echo "   These have interest stored as percentages."
    echo "   After migration, they will be treated as KES amounts."
    echo ""
    echo "   Options:"
    echo "   1. Continue and manually review/update these requests after migration"
    echo "   2. Cancel deployment and handle these requests first"
    echo ""
    read -p "   Continue with migration? (yes/no): " CONTINUE
    
    if [ "$CONTINUE" != "yes" ]; then
        echo "❌ Deployment cancelled by user"
        exit 0
    fi
fi

# Step 3: Run Django migration
echo ""
echo "🔄 Step 3: Running database migration..."
cd ~/public_html || exit 1

python manage.py migrate loans 0031

if [ $? -eq 0 ]; then
    echo "✅ Migration completed successfully"
else
    echo "❌ Migration failed!"
    echo "   To restore: mysql -u xygbfpsg_graz -p'j.ez-xy6##y.rllB' xygbfpsg_loans < $BACKUP_FILE"
    exit 1
fi

# Step 4: Restart application
echo ""
echo "🔄 Step 4: Restarting Django application..."
touch tmp/restart.txt

if [ -f tmp/restart.txt ]; then
    echo "✅ Application restart triggered"
else
    echo "⚠️  Could not trigger restart automatically"
    echo "   Please restart via cPanel: Application → Python Apps → Restart"
fi

# Step 5: Summary
echo ""
echo "============================================================"
echo "✅ Deployment completed successfully!"
echo ""
echo "Changes deployed:"
echo "  ✓ Database field: DECIMAL(5,2) → DECIMAL(12,2)"
echo "  ✓ Form field: 'Custom Interest Rate (%)' → 'Custom Interest Amount (KES)'"
echo "  ✓ Calculation: Now uses direct amount instead of percentage"
echo ""
echo "Next steps:"
echo "  1. Test creating a new rollover request"
echo "  2. Verify the form shows 'Custom Interest Amount (KES)'"
echo "  3. Test approving a rollover with custom interest"
echo "  4. Review any existing pending rollovers (if any)"
echo ""
echo "Backup location: $BACKUP_FILE"
echo "============================================================"
