<?php
/**
 * One-time migration: create wallet_sessions table for automatic Web3 connections.
 * Manual imports remain in crypto_wallets — do not mix the two.
 *
 * Run: php app/scripts/migrate_wallet_sessions.php
 */
require_once __DIR__ . '/../../config/db.php';

$sql = "CREATE TABLE IF NOT EXISTS wallet_sessions (
    id INT AUTO_INCREMENT PRIMARY KEY,
    user_id INT NOT NULL,
    provider VARCHAR(64) NOT NULL,
    address VARCHAR(66) NOT NULL,
    chain_id INT DEFAULT NULL,
    session_json TEXT DEFAULT NULL,
    last_used DATETIME DEFAULT NULL,
    created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
    UNIQUE KEY uniq_user_provider (user_id, provider),
    KEY idx_user (user_id),
    KEY idx_address (address)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci";

if (mysqli_query($link, $sql)) {
    echo "wallet_sessions table ready.\n";
} else {
    echo "Migration failed: " . mysqli_error($link) . "\n";
    exit(1);
}
