<?php
/**
 * Safe migration: show current column type, backup distinct statuses, then alter.
 */
require_once __DIR__ . '/../config/config_enhanced.php';
require_once __DIR__ . '/../classes/Database.php';

try {
    $db = Database::getInstance();
    echo "Current structure for conversations table:\n";
    $rows = $db->fetchAll("SHOW COLUMNS FROM conversations LIKE 'status'");
    print_r($rows);

    $distinct = $db->fetchAll("SELECT DISTINCT status FROM conversations");
    echo "Distinct status values in table:\n";
    print_r(array_column($distinct, 'status'));

    // Ask user confirmation (CLI only)
    if (php_sapi_name() === 'cli') {
        echo "About to alter column 'status' to ENUM('sent','delivered','read','failed','received') DEFAULT 'sent'. Continue? (y/N): ";
        $resp = trim(fgets(STDIN));
        if (strtolower($resp) !== 'y') {
            echo "Aborted by user.\n";
            exit;
        }
    }

    $db->query("ALTER TABLE conversations MODIFY COLUMN status ENUM('sent','delivered','read','failed','received') DEFAULT 'sent'");
    echo "ALTER OK\n";
} catch (Exception $e) {
    echo "ERROR: " . $e->getMessage() . "\n";
}
