<?php
/**
 * Remove Status Column from Unresolved Complaints Table
 * This script removes the 'status' column from the specified table
 * 
 * Usage: php remove_status_from_unresolved_complaints.php [table_name]
 * Example: php remove_status_from_unresolved_complaints.php unresolved_complaints
 */

require_once 'config.php';

// Get table name from command line argument or use default
$tableName = isset($argv[1]) ? trim($argv[1]) : 'unresolved_complaints';

echo "=== Removing Status Column from Table ===\n\n";
echo "Table: $tableName\n\n";

// Check if table exists
$checkTable = $conn->query("SHOW TABLES LIKE '$tableName'");
if ($checkTable->num_rows == 0) {
    echo "✗ Table '$tableName' does not exist\n";
    echo "\nAvailable tables with 'status' column:\n";
    $result = $conn->query("SHOW TABLES");
    while ($row = $result->fetch_array()) {
        $tbl = $row[0];
        $checkStatus = $conn->query("SHOW COLUMNS FROM `$tbl` LIKE 'status'");
        if ($checkStatus->num_rows > 0) {
            echo "  - $tbl\n";
        }
    }
    echo "\nPlease specify the correct table name:\n";
    echo "Usage: php remove_status_from_unresolved_complaints.php [table_name]\n";
    exit(1);
}

echo "✓ Table '$tableName' exists\n";

// Check if status column exists
$checkCol = $conn->query("SHOW COLUMNS FROM `$tableName` LIKE 'status'");
if ($checkCol->num_rows == 0) {
    echo "✓ 'status' column does not exist (already removed or never existed)\n";
    echo "\n=== Done! ===\n";
    exit(0);
}

echo "✓ 'status' column exists\n";
echo "Removing 'status' column...\n";

// Remove the status column
$alterSql = "ALTER TABLE `$tableName` DROP COLUMN status";
if ($conn->query($alterSql)) {
    echo "✓ Successfully removed 'status' column from '$tableName' table\n";
} else {
    echo "✗ Error removing status column: " . $conn->error . "\n";
    exit(1);
}

echo "\n=== Done! ===\n";
echo "The 'status' column has been removed from the '$tableName' table.\n";
?>

