<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');
include_once('connectdb.php');

if (isset($_GET['user_id'])) {
    $user_id = $_GET['user_id'];

    $query = "SELECT wallet_address, account_name, account_number, bank_name, swift_code, routing_number FROM user_wallets WHERE user_id = ?";
    $stmt = $connection->prepare($query);
    $stmt->bind_param('i', $user_id);

    if ($stmt->execute()) {
        // Bind the results to variables
        $stmt->bind_result($wallet_address, $account_name, $account_number, $bank_name, $swift_code, $routing_number);

        // Fetch the results into the variables
        if ($stmt->fetch()) {
            echo json_encode([
                'wallet_address' => $wallet_address,
                'account_name' => $account_name,
                'account_number' => $account_number,
                'bank_name' => $bank_name,
                'swift_code' => $swift_code,
                'routing_number' => $routing_number,
            ]);
        } else {
            // No record found
            echo json_encode([
                'wallet_address' => null,
                'account_name' => null,
                'account_number' => null,
                'bank_name' => null,
                'swift_code' => null,
                'routing_number' => null,
            ]);
        }
    } else {
        echo json_encode(['error' => 'Query execution failed.']);
    }

    $stmt->close();
} else {
    echo json_encode(['error' => 'Invalid request.']);
}
?>
