<?php
require_once __DIR__ . '/app/bootstrap/init.php';
use App\Core\Database;

try {
    $db = Database::getInstance();
    
    echo "Checking columns in 'users' table...\n";
    $stmt = $db->query("DESCRIBE users");
    $columns = $stmt->fetchAll(PDO::FETCH_COLUMN);
    
    if (!in_array('phone_hash', $columns)) {
        echo "Adding 'phone_hash' column...\n";
        $db->exec("ALTER TABLE users ADD COLUMN phone_hash VARCHAR(64) NULL AFTER phone");
        $db->exec("CREATE INDEX idx_phone_hash ON users(phone_hash)");
        echo "Column 'phone_hash' added successfully.\n";
    } else {
        echo "Column 'phone_hash' already exists.\n";
    }

    if (!in_array('email_hash', $columns)) {
        echo "Adding 'email_hash' column...\n";
        $db->exec("ALTER TABLE users ADD COLUMN email_hash VARCHAR(64) NULL AFTER email");
        $db->exec("CREATE INDEX idx_email_hash ON users(email_hash)");
        echo "Column 'email_hash' added successfully.\n";
    } else {
        echo "Column 'email_hash' already exists.\n";
    }
    
    echo "Migration completed.\n";

} catch (Exception $e) {
    echo "Error: " . $e->getMessage() . "\n";
}
