<?php
/**
 * Debug script - Test approve update directly
 * Replace UPDATE_ID with the actual ID you're trying to approve
 */

require_once '../app/Core/init.php';

$update_id = 20; // CHANGE THIS to the ID you're trying to approve
$admin_id = 1;   // Your admin user ID

echo "<h2>Testing Update Approval</h2>";

$db = Database::getInstance();

// Step 1: Check if update exists
echo "<h3>Step 1: Check if update exists</h3>";
$sql = "SELECT * FROM item_updates WHERE id = ?";
$update = $db->fetch($sql, [$update_id]);

if (!$update) {
    echo "❌ Update not found!";
    exit;
}

echo "✓ Update found:<br>";
echo "<pre>";
print_r($update);
echo "</pre>";

// Step 2: Check the status
echo "<h3>Step 2: Check status</h3>";
echo "Status: <strong>{$update['status']}</strong><br>";

if ($update['status'] !== 'pending') {
    echo "❌ Status is not 'pending'. This is why it's failing!<br>";
    echo "Current status: {$update['status']}<br>";
    
    // Reset it to pending for testing
    echo "<br><a href='?reset=1&id={$update_id}'>Click here to reset this update to 'pending'</a>";
    
    if (isset($_GET['reset']) && $_GET['id'] == $update_id) {
        $reset_sql = "UPDATE item_updates SET status = 'pending', reviewed_by = NULL, reviewed_at = NULL WHERE id = ?";
        $db->execute($reset_sql, [$update_id]);
        echo "<br>✓ Reset to pending! <a href='?'>Refresh page</a>";
    }
} else {
    echo "✓ Status is 'pending' - should work!<br>";
    
    // Try to approve it
    echo "<br><a href='?approve=1&id={$update_id}' style='padding: 10px; background: green; color: white; text-decoration: none;'>CLICK HERE TO APPROVE</a>";
    
    if (isset($_GET['approve']) && $_GET['id'] == $update_id) {
        echo "<h3>Step 3: Attempting to approve...</h3>";
        
        try {
            $item = new Item();
            $result = $item->approveUpdate($update_id, $admin_id);
            
            if ($result) {
                echo "✓ <strong style='color: green;'>SUCCESS! Update approved!</strong><br>";
                echo "<a href='?'>Refresh to see changes</a>";
            } else {
                echo "❌ Failed but no exception thrown";
            }
        } catch (Exception $e) {
            echo "❌ <strong style='color: red;'>ERROR: " . $e->getMessage() . "</strong>";
        }
    }
}

// Show all pending updates
echo "<h3>All Pending Updates:</h3>";
$all_pending = $db->fetchAll("SELECT id, item_id, status, created_at FROM item_updates WHERE status = 'pending' ORDER BY created_at DESC");
if (empty($all_pending)) {
    echo "No pending updates found.";
} else {
    echo "<table border='1' cellpadding='5'>";
    echo "<tr><th>ID</th><th>Item ID</th><th>Status</th><th>Created</th><th>Action</th></tr>";
    foreach ($all_pending as $upd) {
        echo "<tr>";
        echo "<td>{$upd['id']}</td>";
        echo "<td>{$upd['item_id']}</td>";
        echo "<td>{$upd['status']}</td>";
        echo "<td>{$upd['created_at']}</td>";
        echo "<td><a href='?id={$upd['id']}'>Test This</a></td>";
        echo "</tr>";
    }
    echo "</table>";
}