<?php
// Quick fix script to disable balance history logging
// Run this if the database truncation error persists

require_once '../config.php';
require_once '../app/classes/AdminAuth.php';

$adminAuth = new AdminAuth($conn);

// Check if admin is authenticated
if (!$adminAuth->isAuthenticated()) {
    die('Unauthorized access');
}

echo "<h2>Balance History Logging Control</h2>";

if (isset($_POST['action'])) {
    if ($_POST['action'] === 'disable') {
        // Update config.php to disable balance history
        $configContent = file_get_contents('../config.php');
        $newConfigContent = str_replace(
            "define('DISABLE_BALANCE_HISTORY', false);",
            "define('DISABLE_BALANCE_HISTORY', true);",
            $configContent
        );
        
        if (file_put_contents('../config.php', $newConfigContent)) {
            echo "<p style='color: green;'>✅ Balance history logging has been DISABLED.</p>";
            echo "<p>This will prevent the database truncation error from occurring.</p>";
        } else {
            echo "<p style='color: red;'>❌ Failed to update config file.</p>";
        }
    } elseif ($_POST['action'] === 'enable') {
        // Update config.php to enable balance history
        $configContent = file_get_contents('../config.php');
        $newConfigContent = str_replace(
            "define('DISABLE_BALANCE_HISTORY', true);",
            "define('DISABLE_BALANCE_HISTORY', false);",
            $configContent
        );
        
        if (file_put_contents('../config.php', $newConfigContent)) {
            echo "<p style='color: green;'>✅ Balance history logging has been ENABLED.</p>";
        } else {
            echo "<p style='color: red;'>❌ Failed to update config file.</p>";
        }
    }
}

// Check current status
$configContent = file_get_contents('../config.php');
$isDisabled = strpos($configContent, "define('DISABLE_BALANCE_HISTORY', true);") !== false;

echo "<p><strong>Current Status:</strong> ";
if ($isDisabled) {
    echo "<span style='color: red;'>DISABLED</span></p>";
    echo "<form method='post' style='margin: 20px 0;'>";
    echo "<input type='hidden' name='action' value='enable'>";
    echo "<button type='submit' style='background: green; color: white; padding: 10px 20px; border: none; border-radius: 5px;'>Enable Balance History Logging</button>";
    echo "</form>";
} else {
    echo "<span style='color: green;'>ENABLED</span></p>";
    echo "<form method='post' style='margin: 20px 0;'>";
    echo "<input type='hidden' name='action' value='disable'>";
    echo "<button type='submit' style='background: red; color: white; padding: 10px 20px; border: none; border-radius: 5px;'>Disable Balance History Logging</button>";
    echo "</form>";
}

echo "<div style='background: #f0f0f0; padding: 15px; margin: 20px 0; border-radius: 5px;'>";
echo "<h3>Instructions:</h3>";
echo "<ul>";
echo "<li><strong>If you're getting database truncation errors:</strong> Click 'Disable' to stop the errors</li>";
echo "<li><strong>After fixing the database schema:</strong> Click 'Enable' to restore logging</li>";
echo "<li><strong>Note:</strong> Disabling will prevent balance history from being recorded, but won't affect balance updates</li>";
echo "</ul>";
echo "</div>";

echo "<p><strong>Important:</strong> Delete this file after use for security.</p>";
?>
