#!/bin/bash

# Restore Script for Suspend Functionality Changes
# This script reverts all changes made to implement the suspend functionality

echo "=== RESTORING SUSPEND FUNCTIONALITY CHANGES ==="
echo "Date: $(date)"
echo ""

# Check if we're in the right directory
if [ ! -f "system/controllers/plan.php" ]; then
    echo "ERROR: Please run this script from the project root directory"
    exit 1
fi

echo "1. Backing up current files before restore..."

# Create backup directory
mkdir -p backups/$(date +%Y%m%d_%H%M%S)
BACKUP_DIR="backups/$(date +%Y%m%d_%H%M%S)"

# Backup current modified files
cp system/controllers/plan.php "$BACKUP_DIR/plan.php.current"
cp ui/ui/plan.tpl "$BACKUP_DIR/plan.tpl.current"
cp system/cron.php "$BACKUP_DIR/cron.php.current"

echo "   ✓ Backed up current files to $BACKUP_DIR"

echo ""
echo "2. Restoring original files..."

# Check if original backups exist
if [ ! -f "backups/original/plan.php" ]; then
    echo "   ⚠️  Original backup not found. Creating from current state..."
    mkdir -p backups/original
    cp system/controllers/plan.php backups/original/plan.php
    cp ui/ui/plan.tpl backups/original/plan.tpl
    cp system/cron.php backups/original/cron.php
    echo "   ✓ Created original backup from current state"
else
    echo "   ✓ Found original backups"
fi

# Restore original files
cp backups/original/plan.php system/controllers/plan.php
cp backups/original/plan.tpl ui/ui/plan.tpl
cp backups/original/cron.php system/cron.php

echo "   ✓ Restored original files"

echo ""
echo "3. Cleaning up temporary files..."

# Remove any temporary files created during implementation
rm -f debug_cron.php
rm -f test_suspend.php

echo "   ✓ Cleaned up temporary files"

echo ""
echo "4. Verifying restoration..."

# Check if files were restored
if [ -f "system/controllers/plan.php" ] && [ -f "ui/ui/plan.tpl" ] && [ -f "system/cron.php" ]; then
    echo "   ✓ All files restored successfully"
else
    echo "   ❌ ERROR: Some files could not be restored"
    exit 1
fi

echo ""
echo "=== RESTORATION COMPLETE ==="
echo "All suspend functionality changes have been reverted."
echo "The system should now be in its original state."
echo ""
echo "Files restored:"
echo "  - system/controllers/plan.php"
echo "  - ui/ui/plan.tpl" 
echo "  - system/cron.php"
echo ""
echo "Current backup saved to: $BACKUP_DIR"
echo "Original backup location: backups/original/"
echo ""
echo "You can now test the original functionality."
