<?php
// heartbeat.php
// Runs in background while protection is happening.
// Sends a "still working" Telegram message every 60 seconds.
// Stops automatically when the flag file is removed by worker.php.

if ($argc < 4) {
    die("Usage: php heartbeat.php <chat_id> <bot_token> <flag_file>\n");
}

$chatId    = $argv[1];
$botToken  = $argv[2];
$flag_file = $argv[3];

$start_time = time();
$interval   = 60; // seconds between each update
$website    = 'https://api.telegram.org/bot' . $botToken;

// Wait first interval before sending the first heartbeat
sleep($interval);

while (file_exists($flag_file)) {
    $elapsed_seconds = time() - $start_time;
    $minutes         = floor($elapsed_seconds / 60);
    $seconds         = $elapsed_seconds % 60;

    $msg  = "⏳ Still working... ({$minutes}m {$seconds}s elapsed)\n";
    $msg .= "🔄 Protection in progress, please wait...\n";
    $msg .= "📌 Tip: Large APKs can take up to 10 minutes.";

    $url = $website . "/sendMessage?chat_id=" . $chatId . "&text=" . urlencode($msg);
    @file_get_contents($url);

    sleep($interval);
}

// Flag file gone = worker finished
exit(0);
?>
