<?php
require_once "config/db.php";

echo "<h2>Fixing Users Table...</h2>";

// Debug: List current columns
echo "<b>Current columns in 'users' table:</b><br>";
$cols = mysqli_query($conn, "SHOW COLUMNS FROM users");
while($c = mysqli_fetch_assoc($cols)) {
    echo "- " . $c['Field'] . "<br>";
}
echo "<br>";

// Check if is_deleted column exists in users
$check_users = mysqli_query($conn, "SHOW COLUMNS FROM users LIKE 'is_deleted'");
if (mysqli_num_rows($check_users) == 0) {
    echo "Adding is_deleted column to users...<br>";
    // Use a simpler query without AFTER
    $sql = "ALTER TABLE users ADD is_deleted TINYINT(1) DEFAULT 0";
    if (mysqli_query($conn, $sql)) {
        echo "✓ Users table updated successfully<br>";
    } else {
        echo "✗ Error updating users table: " . mysqli_error($conn) . "<br>";
    }
} else {
    echo "✓ Users table already has is_deleted column<br>";
}

// Double check products table
$check_products = mysqli_query($conn, "SHOW COLUMNS FROM products LIKE 'is_deleted'");
if (mysqli_num_rows($check_products) == 0) {
    echo "Adding is_deleted column to products...<br>";
    $sql = "ALTER TABLE products ADD is_deleted TINYINT(1) DEFAULT 0";
    if (mysqli_query($conn, $sql)) {
        echo "✓ Products table updated successfully<br>";
    } else {
        echo "✗ Error updating products table: " . mysqli_error($conn) . "<br>";
    }
} else {
    echo "✓ Products table already has is_deleted column<br>";
}

echo "<br><b>Migration completed! You can delete this file now.</b>";
?>
